Blackberry 6: ListField - limit the number of displayed rows?
I have a question about ListField:
If I call setSize(16) then I indicate, how many items this component contains. How do I control the height of this component then?
For example I use the code below to display a TextField and a ListField below, but I also would like to show a ButtonField underneath the ListField:
MyScreen.java:
private MyList presetList = new MyList();
private MyScreen() {
setTitle("Favorites");
BasicFilteredList filterList = new BasicFilteredList();
String[] days = {"Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday","Sunday"};
int uniqueID = 0;
filterList.addDataSet(uniqueID,days,"days",BasicFilteredList.COMPARISON_IGNORE_CASE);
AutoCompleteField autoCompleteField = new 开发者_高级运维AutoCompleteField(filterList);
add(autoCompleteField);
presetListField.setRowHeight(-2);
presetListField.setCallback(presetList);
presetListField.setSize(MyList.items.length());
add(presetListField);
}
MyList.java:
public class MyList implements ListFieldCallback {
public static String items[] = {
"Favorite 01", "Favorite 02", "Favorite 03", "Favorite 04",
"Favorite 11", "Favorite 12", "Favorite 13", "Favorite 14",
"Favorite 21", "Favorite 22", "Favorite 23", "Favorite 24",
"Favorite 31", "Favorite 32", "Favorite 33", "Favorite 34"
};
public void drawListRow(ListField listField, Graphics g, int index, int y, int width) {
Font f = g.getFont();
Font b = f.derive(Font.BOLD, f.getHeight() * 2);
g.setColor(Color.WHITE);
g.drawText(items[index], Display.getWidth()/3, y);
g.drawText("Click to get frequency", Display.getWidth()/3, y + g.getFont().getHeight());
g.setFont(b);
g.setColor(Color.YELLOW);
g.drawText("100." + index, 0, y);
}
public Object get(ListField list, int index) {
return items[index];
}
public int indexOfList(ListField list, String p, int s) {
return -1;
}
public int getPreferredWidth(ListField list) {
return Display.getWidth();
}
}
Thank you! Alex
UPDATE:
Maybe ListField is supposed to always show all its items and you can't limit its view to few rows?
This is probably not exactly what you request for, but could you try to put your button into the status container of your screen? This would be the easiest way to have the button in the bottom of the screen. However in this case the button will stay there regardless of the list size (number of items).
Class MainScreen
public void setStatus(Field status)
Sets the contents of this screen's status section. This method adds your provided field to this screen's status section, even if it already contains a field. Parameters: status - New status field.
UPDATE:
You are asking why this is as it is. Well, as I see it this was an RIM's attempt to implement some sort of MVC (Model-View-Controller) pattern. Undoubtedly it is not ideal. ListField
is an old core BB API class which has not changed for years. I think if they had a second chance the API would be much better. For instance, on Android lists APIs are much better. I also find ListField.setSize()
a bit ugly, but we can not change this and it is up to us to manually instruct the ListField
that the underlying list has changed its size.
A point about ListFieldCallback
usage. With this approach ListField
itself knows nothing about what list items actually are. It is up to ListFieldCallback
to return a list item object on request or to draw a list item representation on request. Note this happens on request. ListField
asks the ListFieldCallback
to draw only those items which are visible on the screen. This is very important in terms of speed and RAM usage. If your list is 1000 items and each item implies drawing an icon which should be loaded from the network (or just read from the filesystem), then preparing the whole list data at once whould be a show-stopper. But with ListFieldCallback
you are able to process only those items which are requested, so you can implement lazy-loading and your list becomes as quick as possible.
For some simple cases (when your listItem UI representation can be got just by calling listItem.toString()
) there is the ObjectListField
class. This class already implements the ListFieldCallback
, so it displays the list right out of the box.
精彩评论