今天群里有人说删除的时候总是从最后一个listfield删除,而不管当前光标在哪个object,于是空下来写了一下,很简单的实现了。
程序比较简单,数组上下限等判断没有加。
PS:据说必须同时写上myCallback.remove(myList.getSelectedIndex())和myList.delete(myList.getSelectedIndex())才能达到效果。
class testscreen extends MainScreen {
private int i = 3;
ListCallback myCallback = new ListCallback();
private ListField myList;
public testscreen(){
super();
LabelField title = new LabelField("listfieldtest",LabelField.ELLIPSIS|LabelField.USE_ALL_WIDTH);
setTitle(title);
myList = new ListField();
myList.setCallback(myCallback);
String fieldOne = "listone";
String fieldTwo = "listtwo";
String fieldThree = "listthree";
myList.insert(0);
myCallback.insert(fieldOne, 0);
myList.insert(1);
myCallback.insert(fieldTwo, 1);
myList.insert(2);
myCallback.insert(fieldThree, 2);
add(myList);
}
private static class ListCallback implements ListFieldCallback {
private Vector listElements = new Vector();
public void drawListRow(ListField list, Graphics g, int index, int y, int w) {
String text = (String)listElements.elementAt(index);
g.drawText(text, 0, y, 0, w);
}
public Object get(ListField list, int index) {
return listElements.elementAt(index);
}
public int indexOfList(ListField list, String p, int s) {
return listElements.indexOf(p, s);
}
public int getPreferredWidth(ListField list) {
return Graphics.getScreenWidth();
}
public void insert(String toInsert, int index) {
listElements.addElement(toInsert);
}
public void remove(int index){
int i;
for(i=0;i {
System.out.println(i+"="+ (String) listElements.elementAt(i));
}
System.out.println("size====="+listElements.size());
System.out.println("remove==="+listElements.elementAt(index));
listElements.removeElementAt(index);
}
public void rase() {
listElements.removeAllElements();
}
}
private MenuItem newItem = new MenuItem("New", 100, 10) {
public void run() {
myList.insert(i);
myCallback.insert("new",i);
i++;
}
};
private MenuItem romItem = new MenuItem("Remove", 100, 20) {
public void run() {
myCallback.remove(myList.getSelectedIndex());
myList.delete(myList.getSelectedIndex());
}
};
protected void makeMenu(Menu menu, int instance) {
menu.add(newItem);
menu.add(romItem);
}
}