BlackBerry App list
Is a list.
Next we will create a list of instances.
Function: a list containing two rows. Click (or press) a row in the list. A dialog box is displayed, showing the clicked result.
Steps:
1. Create a UiApplication object and initialize the application. For details about UiApplication and Screen, refer to blackberry ui app (1) | blackberry app (1)
2. declare the following fields in the inherited Screen class:
ListField list
The Vector contains the rows in the list.
ListCallback inherits the class from the ListFieldCallback interface.
LIstCallback class
1 class ListCallback implements ListFieldCallback {
2 public void drawListRow (ListField listField, Graphics graphics,
3 int index, int y, int width ){
4 String text = (String) _ listElements. elementAt (index );
5 graphics. drawText (text, 0, y, 0, width );
6}
7
8 public Object get (ListField listField, int index ){
9 // TODO Auto-generated method stub
10 return _ listElements. elementAt (index );
11}
12
13 public int getPreferredWidth (ListField listField ){
14 return Display. getWidth ();
15}
16
17 public int indexOfList (ListField listField, String prefix, int start ){
18 return _ listElements. indexOf (prefix, start );
19}
20
21}
Execute the action in the trackwheelClick method of Screen:
First obtain the index of the selected item, int index = _ listField. getSelectedIndex ();
Then obtain the currently selected item, String s = (String) _ listField. getCallback (). get (_ listField, index); The get method returns objects of the Object type, which can be converted according to the actual type.
1 public boolean trackwheelClick (int status, int time ){
2 int index = _ listField. getSelectedIndex ();
3 String s = (String) _ listField. getCallback (). get (_ listField, index );
4 Dialog. alert (s );
5 return true;
6}
Complete code:
Code 1 package wei. bb. demo. app;
2
3 import java. util. Vector;
4
5 import net. rim. device. api. system. Display;
6 import net. rim. device. api. ui. Graphics;
7 import net. rim. device. api. ui. component. Dialog;
8 import net. rim. device. api. ui. component. ListField;
9 import net. rim. device. api. ui. component. ListFieldCallback;
10 import net. rim. device. api. ui. container. MainScreen;
11
12 public class MenuScreen extends MainScreen {
13
14 private ListField _ listField;
15 private Vector _ listElements;
16 private ListCallback _ callback;
17 public MenuScreen (){
18 this. setTitle ("Demo For BB ");
19 _ listElements = new Vector ();
20 _ listField = new ListField ();
21 _ listField. setCallback (new ListCallback ());
22 add (_ listField );
23 initializeList ();
24}
25
26
27 public boolean trackwheelClick (int status, int time ){
28 int index = _ listField. getSelectedIndex ();
29 String s = (String) _ listField. getCallback (). get (_ listField, index );
30 Dialog. alert (s );
31 return true;
32}
33
34 private void initializeList ()
35 {
36 String itemOne = "List item one ";
37 String itemTwo = "List item two ";
38 _ listElements. addElement (itemOne );
39 _ listElements. addElement (itemTwo );
40 _ listField. setSize (_ listElements. size ());
41}
42
43 class ListCallback implements ListFieldCallback {
44 public void drawListRow (ListField listField, Graphics graphics,
45 int index, int y, int width ){
46 String text = (String) _ listElements. elementAt (index );
47 graphics. drawText (text, 0, y, 0, width );
48}
49
50 public Object get (ListField listField, int index ){
51 // TODO Auto-generated method stub
52 return _ listElements. elementAt (index );
53}
54
55 public int getPreferredWidth (ListField listField ){
56 return Display. getWidth ();
57}
58
59 public int indexOfList (ListField listField, String prefix, int start ){
60 return _ listElements. indexOf (prefix, start );
61}
62
63}
64}
65