我准备了一个简单的测试案例来证明我的问题。
它只是一个文件(CheckMenu.java,如下所示)并且可以立即运行。
一旦用户通过选择2个MenuItems中的一个来更改排序方法,如何重绘基础KeywordFilterField?我试过调用 myList.updateList()和 myList.invalidate() - 它没有帮助 - myList项目没有重新组织。
我也想知道,使用什么而不是弃用的MenuItem.setText(String)?
package mypackage;
import net.rim.device.api.system.*;
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.util.Comparator;
import net.rim.device.api.util.StringUtilities;
import net.rim.device.api.collection.util.*;
public class CheckMenu extends UiApplication {
public static void main(String[] args) {
CheckMenu myApp = new CheckMenu();
myApp.enterEventDispatcher();
}
public CheckMenu() {
pushScreen(new MyScreen());
}
}
class MyScreen extends MainScreen {
KeywordFilterField myList = new KeywordFilterField();
MyItemList myItems = new MyItemList();
public MyScreen() {
setTitle(myList.getKeywordField());
myItems.doAdd(new MyItem(1, "Eins"));
myItems.doAdd(new MyItem(2, "Zwei"));
myItems.doAdd(new MyItem(3, "Drei"));
myItems.doAdd(new MyItem(4, "Vier"));
myList.setSourceList(myItems, new MyItem.MyProvider());
// XXX commenting the line below does not help
myList.setCallback(new MyListFieldCallback());
add(myList);
}
private MenuItem numMenu = new MenuItem("num sort", 0, 0) {
public void run() {
MyItem.NUMERIC_SORT = true;
Status.show("Use " + toString());
setText("num sort \u221A");
alphaMenu.setText("alpha sort");
myList.updateList();
}
};
private MenuItem alphaMenu = new MenuItem("alpha sort", 1, 0) {
public void run() {
MyItem.NUMERIC_SORT = false;
Status.show("Use " + toString());
setText("alpha sort \u221A");
numMenu.setText("num sort");
myList.updateList();
}
};
protected void makeMenu(Menu menu, int instance) {
super.makeMenu(menu, instance);
menu.add(numMenu);
menu.add(alphaMenu);
}
private class MyListFieldCallback implements ListFieldCallback {
public void drawListRow(ListField list, Graphics g, int index, int y, int width) {
Object obj = ((KeywordFilterField)list).getElementAt(index);
if(obj != null && obj instanceof MyItem) {
MyItem item = (MyItem) obj;
g.drawText(item.toString(), 20 * (1 + index), y);
} else if(index == 0) {
g.drawText(list.getEmptyString(), 0, y);
}
}
public Object get(ListField list, int index) {
return null;
}
public int getPreferredWidth(ListField list) {
return 0;
}
public int indexOfList(ListField list, String prefix, int start) {
return 0;
}
}
}
class MyItemList extends SortedReadableList {
public MyItemList() {
super(new MyItem.MyComparator());
}
protected void doAdd(Object obj) {
super.doAdd(obj);
}
protected boolean doRemove(Object obj) {
return super.doRemove(obj);
}
}
class MyItem {
public static boolean NUMERIC_SORT;
private int _num;
private String _name;
public MyItem(int num, String name) {
_num = num;
_name = name;
}
public String toString() {
return _num + ": " + _name;
}
static class MyComparator implements Comparator {
public int compare(Object obj1, Object obj2) {
if (! (obj1 instanceof MyItem && obj2 instanceof MyItem))
throw new IllegalArgumentException("Cannot compare non-MyItems");
MyItem item1 = (MyItem) obj1;
MyItem item2 = (MyItem) obj2;
if (MyItem.NUMERIC_SORT) {
if (item1._num == item2._num)
return 0;
return (item1._num > item2._num ? 1 : -1);
}
return item1._name.compareTo(item2._name);
}
}
static class MyProvider implements KeywordProvider {
public String[] getKeywords(Object obj) {
if (obj instanceof MyItem) {
MyItem item = (MyItem) obj;
return new String[]{ Integer.toString(item._num), item._name };
}
return null;
}
}
}
更新:我已根据Arhimed的建议更新了代码以使用makeMenu()。 我还发布了我的问题at the BlackBerry forum。
谢谢! 亚历
答案 0 :(得分:1)
也许我不明白你的问题.....但正如我所见,这段代码在模拟器上工作正常
import net.rim.device.api.system.*;
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.util.Comparator;
import net.rim.device.api.util.StringUtilities;
import net.rim.device.api.collection.util.*;
public class CheckMenu extends UiApplication {
public static void main(String[] args) {
CheckMenu myApp = new CheckMenu();
myApp.enterEventDispatcher();
}
public CheckMenu() {
pushScreen(new MyScreen());
}
}
class MyScreen extends MainScreen {
KeywordFilterField myList = new KeywordFilterField();
MyItemList myItems = new MyItemList();
public MyScreen() {
setTitle(myList.getKeywordField());
myItems.doAdd(new MyItem(1, "Eins"));
myItems.doAdd(new MyItem(2, "Zwei"));
myItems.doAdd(new MyItem(3, "Drei"));
myItems.doAdd(new MyItem(4, "Vier"));
myList.setSourceList(myItems, new MyItem.MyProvider());
// XXX commenting the line below does not help
myList.setCallback(new MyListFieldCallback());
add(myList);
}
private MenuItem numMenu = new MenuItem("num sort", 0, 0) {
public void run() {
MyItem.NUMERIC_SORT = true;
Status.show("Use " + toString());
setText("num sort \u221A");
alphaMenu.setText("alpha sort");
myItems.sort();
myList.updateList();
}
};
private MenuItem alphaMenu = new MenuItem("alpha sort", 1, 0) {
public void run() {
MyItem.NUMERIC_SORT = false;
Status.show("Use " + toString());
setText("alpha sort \u221A");
numMenu.setText("num sort");
myItems.sort();
myList.updateList();
}
};
protected void makeMenu(Menu menu, int instance) {
super.makeMenu(menu, instance);
menu.add(numMenu);
menu.add(alphaMenu);
}
private class MyListFieldCallback implements ListFieldCallback {
public void drawListRow(ListField listField, Graphics g, int index, int y, int width) {
Object obj = ((KeywordFilterField)listField).getElementAt(index);
if(obj != null && obj instanceof MyItem) {
MyItem item = (MyItem) obj;
g.drawText(item.toString(), 20 * (1 + index), y);
} else if(index == 0) {
g.drawText("ssssssss", 0, y);
}
}
public Object get(ListField listField, int index) {
return null;
}
public int getPreferredWidth(ListField listField) {
return 0;
}
public int indexOfList(ListField listField, String prefix, int start) {
return 0;
}
}
}
class MyItemList extends SortedReadableList {
public MyItemList() {
super(new MyItem.MyComparator());
}
protected void doAdd(Object obj) {
super.doAdd(obj);
}
protected boolean doRemove(Object obj) {
return super.doRemove(obj);
}
}
class MyItem {
public static boolean NUMERIC_SORT;
private int _num;
private String _name;
public MyItem(int num, String name) {
_num = num;
_name = name;
}
public String toString() {
return _num + ": " + _name;
}
static class MyComparator implements Comparator {
public int compare(Object obj1, Object obj2) {
if (! (obj1 instanceof MyItem && obj2 instanceof MyItem))
throw new IllegalArgumentException("Cannot compare non-MyItems");
MyItem item1 = (MyItem) obj1;
MyItem item2 = (MyItem) obj2;
if (MyItem.NUMERIC_SORT) {
if (item1._num == item2._num)
return 0;
return (item1._num > item2._num ? 1 : -1);
}
return item1._name.compareTo(item2._name);
}
}
static class MyProvider implements KeywordProvider {
public String[] getKeywords(Object obj) {
if (obj instanceof MyItem) {
MyItem item = (MyItem) obj;
return new String[]{ Integer.toString(item._num), item._name };
}
return null;
}
}
}
答案 1 :(得分:0)
1)。如何修改MenuItem文本? ID 喜欢显示/隐藏复选标记 符号\ u221A - 取决于哪个 目前选择排序顺序 用户。 setText(String text) 方法已被弃用,我不确定 如何使用setText(StringProvider 文本)?
而不是使用addMenuItem(myMenu1);
覆盖makeMenu(Menu menu, int instance)
:
protected void makeMenu(Menu menu, int instance) {
super.makeMenu(menu, instance);
// add menu items on-the-fly
// generating their labels in accordance to some conditions
menu.add(new MenuItem("any label", 1, 1) {
public void run() {
// do smth useful
}
});
}
2)。一旦用户通过选择2中的1来更改排序方法 MenuItems,我该如何重绘 底层KeywordFilterField?我有 试着调用myList.updateList()和 myList.invalidate() - 它没有帮助。
尝试注释掉myList.setCallback(new MyListFieldCallback());
行。我相信KeywordFilterField
已经以某种特定方式实现了此回调,因此通过设置自定义ListFieldCallback
,您可以打破默认/预期行为。
更新:
完全删除private MenuItem alphaMenu
和private MenuItem numMenu
字段。即时生成这些菜单项:
protected void makeMenu(Menu menu, int instance) {
super.makeMenu(menu, instance);
String label = "num sort" + (MyItem.NUMERIC_SORT ? " \u221A" : "");
menu.add(new MenuItem(label, 0, 0) {
public void run() {
MyItem.NUMERIC_SORT = true;
Status.show("Use " + toString());
// setText("num sort \u221A");
// alphaMenu.setText("alpha sort");
myList.updateList();
}
});
label = "alpha sort" + (MyItem.NUMERIC_SORT ? "" : " \u221A");
menu.add(new MenuItem(label, 1, 0) {
public void run() {
MyItem.NUMERIC_SORT = false;
Status.show("Use " + toString());
// setText("alpha sort \u221A");
// numMenu.setText("num sort");
myList.updateList();
}
});
}
关于主要问题的更新:
class MyScreen extends MainScreen {
KeywordFilterField myList = new KeywordFilterField();
MyItemList myItems = new MyItemList();
public MyScreen() {
setTitle(myList.getKeywordField());
myItems.doAdd(new MyItem(1, "Eins"));
myItems.doAdd(new MyItem(2, "Zwei"));
myItems.doAdd(new MyItem(3, "Drei"));
myItems.doAdd(new MyItem(4, "Vier"));
myList.setSourceList(myItems, new MyItem.MyProvider());
// XXX commenting the line below does not help
myList.setCallback(new MyListFieldCallback());
add(myList);
}
protected void makeMenu(Menu menu, int instance) {
super.makeMenu(menu, instance);
String label = "num sort" + (MyItem.NUMERIC_SORT ? " \u221A" : "");
menu.add(new MenuItem(label, 0, 0) {
public void run() {
MyItem.NUMERIC_SORT = true;
Status.show("Use " + toString());
// setText("num sort \u221A");
// alphaMenu.setText("alpha sort");
myItems.setComparator(new MyItem.MyComparator());
myList.updateList();
}
});
label = "alpha sort" + (MyItem.NUMERIC_SORT ? "" : " \u221A");
menu.add(new MenuItem(label, 1, 0) {
public void run() {
MyItem.NUMERIC_SORT = false;
Status.show("Use " + toString());
// setText("alpha sort \u221A");
// numMenu.setText("num sort");
myItems.setComparator(new MyItem.MyComparator());
myList.updateList();
}
});
}
// .. the rest of the code is the same