This is my code for my implementation of a ListField. It creates a list; however, it is empty even though I thought I add Friends from vectorFriends. I’m very new to developing blackberry apps, so I’m sure my error is pretty obvious. Can anyone tell me why my list is empty?
public class HomeScreen extends MainScreen implements FieldChangeListener, ListFieldCallback{
private ListField listFriends;
private Vector vectorFriends;
private Friend _selectedPerson = null;
public HomeScreen(){
vectorFriends = User.getMyUser().getFriends();
this.add(new LabelField("Friends"));
listFriends = new ListField(vectorFriends.size());
listFriends.setCallback(this);
this.add(listFriends)
}
//implemented ListFieldCallback methods
//draw current row
public void drawListRow(ListField list, Graphics g, int index, int y, int w) {
Friend personToDraw = (Friend) this.get(list, index);
int drawColor = Color.BLACK;
g.setColor(drawColor);
g.drawText(personToDraw.getFullName(), 0, y, 0, w);
}
// get the selected index from the correct Vector
public Object get(ListField list, int index) {
return vectorFriends.elementAt(index);
}
public void insert(String toInsert, int index) {
vectorFriends.insertElementAt(toInsert, index);
}
public int getPreferredWidth(ListField list) {
return Display.getWidth();
}
public int indexOfList(ListField listField, String prefix, int start) {
// Not a correct implementation - this is really just commented out
return start;
}
public int getPreferredWidth(ListField list) {
return Display.getWidth();
}
public int indexOfList(ListField listField, String prefix, int start) {
// Not a correct implementation - this is really just commented out
return start;
}
Is vectorFriends empty perhaps?
Also, your insert() method is adding String objects to the vector, while the drawListRow() method is casting to Friend. These are inconsistent.