首页 » 黑莓开发 » 黑莓上ListField的增加和删除

黑莓上ListField的增加和删除

5655 0

今天群里有人说删除的时候总是从最后一个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);
}
}

文章评分1次,平均分5.0

本文原始地址:https://www.tiandiyoyo.com/2009/12/%e9%bb%91%e8%8e%93%e4%b8%8alistfield%e7%9a%84%e5%a2%9e%e5%8a%a0%e5%92%8c%e5%88%a0%e9%99%a4/
本站所有文章,除了特别注明外,均为本站原创,转载请注明出处来自www.tiandiyoyo.com

您可能还会对以下文章感兴趣:

评论前先开启评论开关: