这是我实现ListField的代码。它创建一个列表;但是,即使我认为我从vectorFriends
添加好友,它也是空的。我是开发黑莓应用程序的新手,所以我确定我的错误非常明显。谁能告诉我为什么我的清单是空的?
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;
}
答案 0 :(得分:2)
vectorFriends也许是空的吗?
此外,您的insert()方法正在向向量添加String对象,而drawListRow()方法正在转换为Friend。这些是不一致的。
答案 1 :(得分:1)
每当vectorFriends改变时,您都应该致电ListField.setSize():
Friends.java:
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.decor.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.image.*;
import java.util.*;
public class Friends extends UiApplication {
public static void main(String[] args) {
Friends app = new Friends();
app.enterEventDispatcher();
}
public Friends () {
pushScreen(new HomeScreen());
}
}
class HomeScreen extends MainScreen implements FieldChangeListener, ListFieldCallback {
private ListField listFriends;
private Vector vectorFriends = new Vector();
public HomeScreen() {
vectorFriends.addElement("Alice");
vectorFriends.addElement("Bob");
vectorFriends.addElement("Carol");
vectorFriends.addElement("Dave");
vectorFriends.addElement("Eve");
setTitle(new LabelField("Friends"));
listFriends = new ListField(vectorFriends.size());
listFriends.setCallback(this);
listFriends.setEmptyString("You have no friends", 0);
add(listFriends);
ButtonField kill = new ButtonField("Kill");
kill.setChangeListener(this);
add(kill);
}
public void fieldChanged(Field field, int context) {
if (vectorFriends.size() > 0) {
vectorFriends.removeElementAt(0);
listFriends.setSize(vectorFriends.size()); // IMPORTANT
}
}
//implemented ListFieldCallback methods
//draw current row
public void drawListRow(ListField list, Graphics g, int index, int y, int w) {
String personToDraw = (String) this.get(list, index);
int drawColor = Color.RED;
g.setColor(drawColor);
g.drawText(personToDraw, 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) {
return vectorFriends.indexOf(prefix, start);
}
}
答案 2 :(得分:0)
您似乎没有在列表中调用setSize()
。