这个程序是可以运行的,功能有欠缺地方:选择 "余乐征 "时显示了 "ai.png "可是我选择 "小彭 "后它不是显示 "xiaopeng.png "仍然是 "ai.png ",我知道这是Alert的原因,可是我又不知道怎么让两个选项显示不同图片.救助大虾们。
//listalert.java
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.*;
public class listalert extends MIDlet implements CommandListener {
private Display display;
private Command cmdExit;
private List list;
private Alert alert;
private Image image;
private Image imagexiaopeng;
public listalert() {
// TODO 自动生成构造函数存根
display=Display.getDisplay(this);
cmdExit=new Command( "退出 ",Command.SCREEN,1);
list=new List( "List ",List.IMPLICIT);
try{
image=Image.createImage( "/ai.png ");
}
catch(Exception e)
{
}
try{
imagexiaopeng=Image.createImage( "/xiaopeng.png ");
}
catch(Exception ex)
{
}
list.append( "余乐征 ",null);
list.append( "小锐 ",null);
list.addCommand(cmdExit);
list.setCommandListener(this);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO 自动生成方法存根
}
protected void pauseApp() {
// TODO 自动生成方法存根
}
protected void startApp() throws MIDletStateChangeException {
// TODO 自动生成方法存根
display.setCurrent(list);
}
public void commandAction(Command c,Displayable d)
{
if(c==List.SELECT_COMMAND)
{
String select=list.getString(list.getSelectedIndex());
alert=new Alert(null,select,imagexiaopeng,null);
alert.setTimeout(Alert.FOREVER);
alert.setType(AlertType.INFO);
display.setCurrent(alert);
}
if(c==cmdExit)
{
notifyDestroyed();
}
}
}
------解决方案--------------------------------------------------------
if (c == List.SELECT_COMMAND)
{
List tmp=(List)d;
switch(tmp.getSelectedIndex())
{case 0:
String select = list.getString(list.getSelectedIndex());
alert = new Alert(null, select, image, null);
alert.setTimeout(Alert.FOREVER);
alert.setType(AlertType.INFO);
display.setCurrent(alert);
break;
case 1:
String select1 = list.getString(list.getSelectedIndex());
alert = new Alert(null, select1, imagexiaopeng, null);
alert.setTimeout(Alert.FOREVER);
alert.setType(AlertType.INFO);
display.setCurrent(alert);
break;
}