小弟初学,很多东西没有头绪,关于列表添加操作时,只能进行一次的添加功能,第二次就不行了,代码如下:
package test;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.*;
public class View extends MIDlet implements CommandListener{
private Display display = null;
private Form form = null;
private List list = null;
private TextField text = new TextField( "商品名 ", " ",16,TextField.ANY);
private Command cmdADD = new Command( "添加 ",Command.SCREEN,1);
private Command cmdROV = new Command( "删除 ",Command.SCREEN,1);
private Command cmdOK = new Command( "完成 ",Command.OK,1);
private Command cmdBACK = new Command( "返回 ",Command.BACK,1);
private Command cmdEXIT = new Command( "退出 ",Command.EXIT,1);
public View() {
super();
init();
// TODO 自动生成构造函数存根
}
public void init() {
list = new List( "商品名称 ",List.IMPLICIT);
list.addCommand(cmdADD);
list.addCommand(cmdROV);
list.addCommand(cmdOK);
list.addCommand(cmdBACK);
list.addCommand(cmdEXIT);
}
protected void startApp() throws MIDletStateChangeException {
// TODO 自动生成方法存根
display = Display.getDisplay(this);
list.setCommandListener(this);
display.setCurrent(list);
}
protected void pauseApp() {
// TODO 自动生成方法存根
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO 自动生成方法存根
//System.out.println(text.getString());
}
public void commandAction(Command arg0, Displayable arg1) {
// TODO 自动生成方法存根
if(arg1.equals(list)) {
if(arg0.equals(cmdADD)) {
form = new Form( "添加 ");
form.append(text);
form.addCommand(cmdOK);
form.addCommand(cmdBACK);
form.setCommandListener(this);
display.setCurrent(form);
}
}
if(arg1.equals(form)) {
if(arg0.equals(cmdOK)) {
init();
list.append(text.getString(),null);
list.setCommandListener(this);
display.setCurrent(list);
}
}
}
}
请各位哥哥介绍几本好书,或者一些学习的方法,不胜感激
------解决方案--------------------------------------------------------
修改后的代码片段为:
if (arg1.equals(form))
{
if (arg0.equals(cmdOK))
{
init();
list.append(text.getString(), null);
form.deleteAll();//注意这里,删除
list.setCommandListener(this);
display.setCurrent(list);
}
}
测试显示正常