itemStateChanged不触发CheckBox的改变吗?我这是个简单的二级联动菜单,怎么选择了省份不能将相应的城市名插入choicegroup中?而且我通过打印发现在我改变省份的时候根本就没触发itemStateChanged方法啊
- Java code
import javax.microedition.lcdui.ChoiceGroup;import javax.microedition.lcdui.Command;import javax.microedition.lcdui.CommandListener;import javax.microedition.lcdui.Display;import javax.microedition.lcdui.Displayable;import javax.microedition.lcdui.Form;import javax.microedition.lcdui.Item;import javax.microedition.lcdui.ItemStateListener;import javax.microedition.midlet.MIDlet;public class CityMidlet extends MIDlet implements CommandListener,ItemStateListener{ private Display display; private Form form; private Command back; private ChoiceGroup province; private ChoiceGroup city; private String[][] provinceAndCity; public CityMidlet() { display = Display.getDisplay(this); provinceAndCity = new String[][]{{"直辖市","北京","上海","天津","重庆"},{"特别行政区","香港","澳门"},{"黑龙江","哈尔滨","齐齐哈尔","牡丹江","大庆","伊春","双鸭山","鹤岗","鸡西","佳木斯","七台河","黑河","绥化","大兴安岭"}}; form = new Form("新增城市"); back = new Command("返回",Command.BACK,1); province = new ChoiceGroup("省份:",ChoiceGroup.POPUP); city = new ChoiceGroup("城市:",ChoiceGroup.POPUP); province.append("请选择", null); for(int i = 0; i < provinceAndCity.length; i++) province.append(provinceAndCity[i][0], null); city.append("请选择", null); form.append(province); form.append(city); form.addCommand(back); form.setCommandListener(this); } public void startApp(){ display.setCurrent(form); } public void pauseApp(){ } public void destroyApp(boolean arg0){ } public void itemStateChanged(Item item){ if(item == province){ int selected = province.getSelectedIndex(); System.out.println(selected); if(selected == 0) return; else{ city.deleteAll(); city.append("请选择", null); for(int i = 1; i < provinceAndCity[selected - 1].length; i++) city.append(provinceAndCity[selected - 1][i], null); } } } public void commandAction(Command c, Displayable dpa) { }}
------解决方案--------------------------------------------------------
Form.setItemStateListener(ItemStateListener)
添加进去,要让程序监听
------解决方案--------------------------------------------------------
测了没问题,加上监听后是可以监听到的