Blackberry: select an item from the list, return to the previous screen

I prepared a very short test case (hereinafter) for my question.

When I click the button, I want to display a list of lines on a new screen.

After the user selects one item in the list, the previous screen should be displayed again, and the button label should be set to the selected line.

screenshot

My 2 problems:

  • From inside the menu, I don’t know how to put the displayed screen.
  • How to transfer the selected item from one screen to another (if I do not want to enter a public variable / method on the first screen as a workaround)

Please provide the necessary changes for my src \ mypackage \ MyList.java :

package mypackage; import java.util.*; import net.rim.device.api.collection.*; import net.rim.device.api.collection.util.*; 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.ui.decor.*; import net.rim.device.api.util.*; import net.rim.device.internal.i18n.*; public class MyList extends UiApplication implements FieldChangeListener { MyScreen myScreen = new MyScreen(); public static void main(String args[]) { MyList app = new MyList(); app.enterEventDispatcher(); } public MyList() { MainScreen titleScreen = new MainScreen(); titleScreen.setTitle("Click the button:"); // TODO change the label of this button (see below) ButtonField myButton = new ButtonField("Show the list", ButtonField.CONSUME_CLICK); myButton.setChangeListener(this); titleScreen.add(myButton); pushScreen(titleScreen); } public void fieldChanged(Field field, int context) { pushScreen(myScreen); } } class MyScreen extends MainScreen { ObjectListField myList = new ObjectListField(); public MyScreen() { setTitle("Select an item below:"); 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? } }; } 

Thanks! Alex

0
source share
1 answer

Use the callback template:

 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 // eg 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); } }; } 
0
source

All Articles