黑莓手机应用 列表
如图所示的东西就是一个列表
下面我们做一个列表的实例。
功能:一个包含两行的列表,点击(或按下)列表中的某行时,弹出一个对话框显示单击的结果
步骤:
1. 新建UiApplication对象,初始化应用。UiApplication和Screen可参考 blackberry ui app(1) |黑莓手机应用(1)
2. 在继承的Screen类中声明分别为下面类型的三个字段:
ListField 列表
Vector 容纳列表的行
ListCallback 继承自ListFieldCallback接口的类
LIstCallback类
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 }
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 }
在 Screen 的trackwheelClick方法中执行动作:
首先获取当前选中项的索引,int index = _listField.getSelectedIndex();
然后取得当前选中的项,String s = (String)_listField.getCallback().get(_listField, index); get方法返回的是Object类型的对象,可根据实际类型进行实际转换
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 }
2 int index = _listField.getSelectedIndex();
3 String s = (String)_listField.getCallback().get(_listField, index);
4 Dialog.alert(s);
5 return true;
6 }
完整代码:
代码
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
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