Blackberry:选择列表中的项目,返回上一屏幕
我准备了一个非常简短的测试用例。下面是我的问题 单击按钮时,我希望在新屏幕中显示字符串列表 用户选择列表中的一个项目后,应再次显示上一个屏幕,并将按钮标签设置为所选字符串 我的两个问题是: 在菜单中,我不知道如何弹出当前显示的屏幕 假设我不想在前一个屏幕上引入公共变量/方法作为解决方法,那么如何将所选项目从一个屏幕传递到另一个屏幕 请建议对我的src\mypackage\MyList.java进行必要的更改: 谢谢大家!!Blackberry:选择列表中的项目,返回上一屏幕,blackberry,screen,mainscreen,Blackberry,Screen,Mainscreen,我准备了一个非常简短的测试用例。下面是我的问题 单击按钮时,我希望在新屏幕中显示字符串列表 用户选择列表中的一个项目后,应再次显示上一个屏幕,并将按钮标签设置为所选字符串 我的两个问题是: 在菜单中,我不知道如何弹出当前显示的屏幕 假设我不想在前一个屏幕上引入公共变量/方法作为解决方法,那么如何将所选项目从一个屏幕传递到另一个屏幕 请建议对我的src\mypackage\MyList.java进行必要的更改: 谢谢大家!! Alex使用回调模式: class TitleScreen extend
Alex使用回调模式:
class TitleScreen extends MainScreen {
private ButtonField myButton;
public TitleScreen() {
super();
setTitle("Click the button:");
// TODO change the label of this button (see below)
myButton = new ButtonField("Show the list", ButtonField.CONSUME_CLICK);
myButton.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
OnItemSelectedCallback callback =
new OnItemSelectedCallback() {
public void onItemSelected(String label) {
TitleScreen.this.onItemSelected(label);
}
};
MyScreen myScreen = new MyScreen(callback);
UiApplication.getUiApplication().pushScreen(myScreen);
}
});
add(myButton);
}
private void onItemSelected(String label) {
// this will be called when a menu item is executed on the MyScreen
// e.g. you can call smth like: myButton.setLabel(label);
}
}
interface OnItemSelectedCallback {
void onItemSelected(String label);
}
class MyScreen extends MainScreen {
ObjectListField myList = new ObjectListField();
private final OnItemSelectedCallback onItemSelectedCallback;
public MyScreen(OnItemSelectedCallback onItemSelectedCallback) {
setTitle("Select an item below:");
this.onItemSelectedCallback = onItemSelectedCallback;
myList.set(new String[] { "Item 1", "Item 2", "Item 3", "Item 4", });
add(myList);
addMenuItem(myMenu);
}
private final MenuItem myMenu = new MenuItem("Select item", 0, 0) {
public void run() {
int index = myList.getSelectedIndex();
if (index < 0)
return;
String item = (String) myList.get(myList, index);
Status.show("Selected: " + item);
// TODO how to return to the previous screen here?
// TODO how to call myButton.setLabel(item) here?
// notify the parent screen
onItemSelectedCallback.onItemSelected(item);
// close the current screen
UiApplication.getUiApplication().popScreen(MyScreen.this);
}
};
}
class TitleScreen extends MainScreen {
private ButtonField myButton;
public TitleScreen() {
super();
setTitle("Click the button:");
// TODO change the label of this button (see below)
myButton = new ButtonField("Show the list", ButtonField.CONSUME_CLICK);
myButton.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
OnItemSelectedCallback callback =
new OnItemSelectedCallback() {
public void onItemSelected(String label) {
TitleScreen.this.onItemSelected(label);
}
};
MyScreen myScreen = new MyScreen(callback);
UiApplication.getUiApplication().pushScreen(myScreen);
}
});
add(myButton);
}
private void onItemSelected(String label) {
// this will be called when a menu item is executed on the MyScreen
// e.g. you can call smth like: myButton.setLabel(label);
}
}
interface OnItemSelectedCallback {
void onItemSelected(String label);
}
class MyScreen extends MainScreen {
ObjectListField myList = new ObjectListField();
private final OnItemSelectedCallback onItemSelectedCallback;
public MyScreen(OnItemSelectedCallback onItemSelectedCallback) {
setTitle("Select an item below:");
this.onItemSelectedCallback = onItemSelectedCallback;
myList.set(new String[] { "Item 1", "Item 2", "Item 3", "Item 4", });
add(myList);
addMenuItem(myMenu);
}
private final MenuItem myMenu = new MenuItem("Select item", 0, 0) {
public void run() {
int index = myList.getSelectedIndex();
if (index < 0)
return;
String item = (String) myList.get(myList, index);
Status.show("Selected: " + item);
// TODO how to return to the previous screen here?
// TODO how to call myButton.setLabel(item) here?
// notify the parent screen
onItemSelectedCallback.onItemSelected(item);
// close the current screen
UiApplication.getUiApplication().popScreen(MyScreen.this);
}
};
}