Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
spiirt
Posts: 7
Registered: ‎11-30-2010
My Device: Bold 9700 & Storm 9550
Accepted Solution

Problem with simple scrolling field list class - different behaviour on simulator & device

Hi all

I've written the class for vertical list of fields with some free space between them (at least I wanted it to work in such a manner :smileyhappy: ). Here's the code:

 

public class SpacingFieldList extends Manager {
	private static final int FIELDS_COUNT = 255;
	
	private int managerWidth;
	private int managerHeight;
	private int spacing;
	
	private Field[] fields;
	private int count;
	
	public SpacingFieldList(int width, int height, Font font, int spacing) {
		super(VERTICAL_SCROLL
			| USE_ALL_WIDTH
			| USE_ALL_HEIGHT
			| NON_FOCUSABLE
			);
		managerWidth = width;
		managerHeight = height;

		count = 0;
		fields = new Field[FIELDS_COUNT];
		this.spacing = spacing;
	}
	
	public void add(Field field) {
		if (count < FIELDS_COUNT) {
			fields[count++] = field;
			
			super.add(field);
		} else {
			throw new BufferOverflowException();
		}
	}
	
	public void remove(int index) {
		super.delete(fields[index]);
		for (int i = index; i < count - 1; i++) {
			fields[i] = fields[i + 1];
		}
	}
	
	public void insert(Field field, int index) {
		if (index >= count) {
			add(field);
		} else {
			for (int i = count; i > index; i--) {
				fields[i] = fields[i - 1];
			}
			fields[index] = field;
			add(field);
			count++;
		}
	}
	
	public int getSelectedIndex() {
		Field focused = getFieldWithFocus();
		for (int i = 0; i < count; i++) {
			if (fields[i] == focused) {
				return i;
			}
		}
		return -1;
	}
	
	public Field getSelectedItem() {
		Field focused = getFieldWithFocus();
		for (int i = 0; i < count; i++) {
			if (fields[i] == focused) {
				return fields[i];
			}
		}
		return null;
	}
	
	public void setSelectedIndex(int index) {
		fields[index].setFocus();
	}
	
	public void sublayout(int w, int h) {
		if (managerWidth == 0) {
			managerWidth = w;
		}
		if (managerHeight == 0) {
			managerHeight = h;
		}
		
		if (count != 0) {
			layoutChild(fields[0], managerWidth, managerHeight);
			setPositionChild(fields[0], 0, 0);
			
			for (int i = 1; i < count; i++) {
				layoutChild(fields[i], managerWidth, managerHeight);
				setPositionChild(fields[i], 0, fields[i - 1].getTop() + fields[i - 1].getHeight() + spacing);
			}
		}

		setExtent(managerWidth, managerHeight);
	}
	
	public int getPreferredWidth() {
		return managerWidth;
	}
	
	public int getPreferredHeight() {
		return managerHeight;
	}
}

I’ve tested the class on 9700 simulator and device. I'm adding some buttons to the list, so the content needs to be scrolled. The thing is that on the simulator the scrolling in the list field works fine. When I choose to move the focus down from the last visible field, next one appears on the screen. But on my bold everything looks quite different. The list isn’t scrolling at all and I see only the first n buttons that I’ve added to it. Explain me please, what’s wrong in the list class. Thanks

 

Please use plain text.
Developer
arkadyz
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Problem with simple scrolling field list class - different behaviour on simulator & device

Welcome to the forums!

 

I'm not completely sure what exactly is wrong there (though your sublayout() is the most suspicious place - see below), but here are a few problems I see:

1) there is no count-- in remove(Field), nor any check for count > 0

2) there is no check for count < FIELDS_COUNT in insert(Field)

3) most importantly - there is no actual size calculation nor setVirtualExtent in sublayout()

4) another sublayout() bug - you will never properly layout the fields which are bigger than the height passed to you. Set your available height to Integer.MAX_VALUE >> 1 (taken from VerticalFieldManager) and keep track of how much more you have left. Use that currently available height as the limit in your layoutChild().

 

To fix sublayout(), consider adding something like this at the end:

 

int maxY = 0;
if (count > 0) {
  maxY = fields[count - 1].getExtent().Y2() + spacing;
}
setVirtualExtent(managerWidth, maxY);

 As an alternative to this exercise, consider running field.setMargin(0, 0, spacing, 0) on each field and adding the fields to a regular VerticalFieldManager(VERTICAL_SCROLL).

 

There are also some less-than-obvious points when using this gadget within a MainScreen - you might want to read about it in the following article:

MainScreen explained

 

I hope to get some spare time towards Christmas - I'll start an article on scrolling, as it seems a very confusing subject in BlackBerry world.

 

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Please use plain text.
New Contributor
spiirt
Posts: 7
Registered: ‎11-30-2010
My Device: Bold 9700 & Storm 9550

Re: Problem with simple scrolling field list class - different behaviour on simulator & device

arkadyz

Thanks a lot, your advice on setVirtialExtent did help. I'll fix the bugs that you mentioned. I'm just learning, and this will be a good lesson. :smileyhappy:

Please use plain text.