J2ME学习笔记(4)--List类的学习
List类在用户显示屏幕上实现了一个选项列表,它实现了Choice接口(Item类的子类Choice也实现该接口)。
List类的构造方法有两种形式,一种是 List(Stirng title, int listTpye)
另一种是List(String title, int listTpye, String[] stringElements, Image[] imageElements)
构造方法定义的listType 在List类中定义了从Choice接口中继承的3个int 类型的字段:
EXCLUSIVE:用户在所有选项中只能选择一项,用户选择一个选项时,前面选择的将被取消,焦点转移到当前选择的选项上,在选择或取消选择是不会触分事件。
MULTIPLE:选择操作会固定焦点元素的选择状态,应用程序不会获得通知,这种类似于复选框,用户可以选择0或多个元素,选择动作不会触发事件。
IMPLICIT:与前面两种不同,对于这种类型的List ,如果设置了CommandListener,选择操用会立即被通知到应用程序。在这个List的作何CommandListener被调用之前,获得焦点的元素将被选择,SELECT_COMMAND 是用于作事件通知的固有参数。
EXCLUSIVE 和MULTIPL类型的List还必须注册到少一个Command,并与一个事件监听连接起来,使用方法和其他的Displayable类相似。
int | append(String strItem, Image image) | 增加一个元素到List |
void | delete(int elementum) | 删除由elementum所指定的元至素 |
int | getSelectFlags(boolean[] selectedArray) | 查询list元素中的状态以布尔数组的形式返回 |
Boolean | isSelected(int index) | 返回索引号为index的元素是否被选 |
void | setSelectFlags(boolean[] selectedArray) | 设置list中所有的选择状态 |
一个简单的例子
package demo;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/*
* List类的例子(三种类型EXCLUSIVE MULTIPLE IMPLICIT)
*/
public class ListExample extends MIDlet implements CommandListener
{
private Command exitCommand;
private Command backCommand;
private List mainList;
private List exclusiveList;
private List multipleList;
private List implicitList;
private Display display;
public ListExample()
{
display = Display.getDisplay(this);
exitCommand = new Command("退出", Command.EXIT, 1);
backCommand = new Command("后退", Command.BACK, 1);
String[] strArray1 = {
"EXCLUSIVE",
"MULTIPLE",
"IMPLICIT"
};
String[] strArray2 = {
"www.google.com",
"www.sina.com.cn",
"www.pku.edu.cn"
};
/*
* 创建不同类型的List对象
*/
mainList = new List("选择类型", Choice.IMPLICIT, strArray1, null);
mainList.addCommand(exitCommand);
mainList.setCommandListener(this);
exclusiveList = new List("EXCLUSIVE", Choice.EXCLUSIVE ,strArray2, null);
exclusiveList.addCommand(exitCommand);
exclusiveList.addCommand(backCommand);
exclusiveList.setCommandListener(this);
multipleList = new List("MULTIPLE", Choice.MULTIPLE ,strArray2, null);
multipleList.addCommand(exitCommand);
multipleList.addCommand(backCommand);
multipleList.setCommandListener(this);
implicitList = new List("IMPLICIT", Choice.IMPLICIT, strArray2, null);
implicitList.addCommand(exitCommand);
implicitList.addCommand(backCommand);
implicitList.setCommandListener(this);
}
public void startApp()
{
display.setCurrent(mainList);
}
public void pauseApp()
{
}
public void destroyApp(boolean condition)
{
}
public void commandAction(Command command , Displayable displayable)
{
/*因为mainList与implicitList都是同类型的(IMPLICIT)所以只做mainList的SELECT_COMMAND的处理*/
if(displayable.equals(mainList))
{
if(command == List.SELECT_COMMAND)
{
switch(((List)displayable).getSelectedIndex())
{
case 0:
display.setCurrent(exclusiveList);
break;
case 1:
display.setCurrent(multipleList);
break;
case 2:
display.setCurrent(implicitList);
break;
}
}
}
else
{
if( command == backCommand)
{
display.setCurrent(mainList);
}
}
if( command == exitCommand)
{
destroyApp(false);
notifyDestroyed();
}
}
}