日期:2014-05-20  浏览次数:20704 次

多个StringItem添加在form后只显示一个
想把每次选择后的选项显示在StringItem,定义了四个StringItem却只显示一个
[java code]import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class ChoiceGroupMIDlet extends MIDlet implements CommandListener,
ItemStateListener {
private Display display;
private Command exitCommand;
private Form myform;
private ChoiceGroup myExclusiveChoiceGroup;
private ChoiceGroup myMultipleChoiceGroup;
private ChoiceGroup myPopupChoiceGroup;
private StringItem myChoice;
private Image img = null;
private StringItem str01;
private StringItem str02;
private StringItem str03;
private int index;

public ChoiceGroupMIDlet() {
// TODO Auto-generated constructor stub
display = Display.getDisplay(this);
exitCommand = new Command("退出",Command.SCREEN,1);
myform = new Form("ChoiceGroup演示");
try{
img = Image.createImage("/pic.png");
}catch(Exception e){
myform.append("加载图像文件失败");
}
myExclusiveChoiceGroup = new ChoiceGroup("选择性别",ChoiceGroup.EXCLUSIVE);
myMultipleChoiceGroup = new ChoiceGroup("选择学校",ChoiceGroup.MULTIPLE);
myPopupChoiceGroup = new ChoiceGroup("选择婚姻状况",ChoiceGroup.POPUP);
myExclusiveChoiceGroup.append("男", img);
myExclusiveChoiceGroup.append("女", img);
myMultipleChoiceGroup.append("南开大学", img);
myMultipleChoiceGroup.append("天津大学", img);
myMultipleChoiceGroup.append("清华大学", img);
myMultipleChoiceGroup.append("北京大学", img);
myPopupChoiceGroup.append("未婚", img);
myPopupChoiceGroup.append("已婚", img);

myChoice = new StringItem("您选择的内容是:","");
str01 = new StringItem("性别","男");
str02 = new StringItem("学校","");
str03 = new StringItem("婚姻","未婚");

myform.append(myExclusiveChoiceGroup);
myform.append(myMultipleChoiceGroup);
myform.append(myPopupChoiceGroup);

myform.append(myChoice);
//myform.append(str01);
myform.append(str02);
myform.append(str03);
index = myform.append(str01);
System.out.print("插入的位置是:" + index);

myform.addCommand(exitCommand);
myform.setCommandListener(this);
myform.setItemStateListener(this);
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub

}

protected void pauseApp() {
// TODO Auto-generated method stub

}

protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
display.setCurrent(myform);
}

public void commandAction(Command c, Displayable d) {
// TODO Auto-generated method stub
if(c == exitCommand){
try {
destroyApp(false);
} catch (MIDletStateChangeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
notifyDestroyed();
}

}

public void itemStateChanged(Item item) {
// TODO Auto-generated method stub
  if(item == myExclusiveChoiceGroup){
  int selected = myExclusiveChoiceGroup.getSelectedIndex();
  String temp = myExclusiveChoiceGroup.getString(selected);
  str01.setText(temp);
  }
  if(item == myMultipleChoiceGroup){//重复出现之前已经选择的选项
  boolean selected;
  String temp = "";
  for(int i = 0;i < myMultipleChoiceGroup.size();i++){
  selected = myMultipleChoiceGroup.isSelected(i);
  if(selected){
  temp += myMultipleChoiceGroup.getString(i);