ѧԺÊ×Ò³>ÍøÂç±à³Ì>JSP>J2MEÖÐʵÏÖ¿ÉÉìչĿ¼Ê÷TreeList

J2MEÖÐʵÏÖ¿ÉÉìչĿ¼Ê÷TreeList

×÷Õߣº À´Ô´£º Ìí¼Óʱ¼ä£º2006-5-21 19:20:23
¡¡¸½´úÂëÒ»·Ý£¬ÕâÊÇÎÒME×é¼þ¿âÖкÜÔçµÄ°æ±¾ÁË¡£±ðµÄ×é¼þÒÔºóÔÚд¡£Æäʵ×îºÃµÄ·½·¨¾ÍÊÇдCanvas¡£

ExpandList.java

package com.skystudio.ExpandList;

public class ExpandListItem {
¡¡ public ExpandListItem(Object content,String imgPath,String selectImgPath,String Label,int type,boolean ifselected){
¡¡¡¡ this.selectImgPath=selectImgPath;
¡¡¡¡ this.imagePath=imgPath;
¡¡¡¡ this.content=content;
¡¡¡¡ this.label=Label;
¡¡¡¡ this.type=type;
¡¡¡¡ this.ifselected=ifselected;
¡¡ }
¡¡ /**
¡¡ * ĬÈÏͼƬ
¡¡ */
¡¡ private String imagePath="";
¡¡ /*
¡¡ * ¼¤»îͼƬ,Èç¹ûΪ¿Õ˵Ã÷´ËͼƬÎÞЧ
¡¡ */
¡¡ private String selectImgPath=null;
¡¡ /**
¡¡ * ×é
¡¡ */
¡¡ public static int GROUP=1;
¡¡ /**
¡¡ * ¼Ç¼
¡¡ */
¡¡ public static int ITEM=0;
¡¡ /**
¡¡ * ÊÇ·ñÑ¡ÖÐ
¡¡ */
¡¡ private boolean ifselected=false;
¡¡ /**
¡¡ * ÏÔʾLabel
¡¡ */
¡¡ private String label;
¡¡ /**
¡¡ * ÀàÐÍ£º×飬¼Ç¼
¡¡ */
¡¡ private int type;
¡¡ /**
¡¡ * ´æ´¢µÄ¶ÔÏó
¡¡ */
¡¡ private Object content;
¡¡¡¡ public Object getContent() {
¡¡¡¡¡¡ return content;
¡¡¡¡ }
¡¡ public void setContent(Object content) {
¡¡¡¡ this.content = content;
¡¡ }
¡¡ public String getLabel() {
¡¡¡¡ return label;
¡¡ }
¡¡ public void setLabel(String label) {
¡¡¡¡ this.label = label;
¡¡ }
¡¡ public int getType() {
¡¡¡¡ return type;
¡¡ }
¡¡ public void setType(int type) {
¡¡¡¡ this.type = type;
¡¡ }
¡¡ public boolean Ifselected() {
¡¡¡¡ return ifselected;
¡¡ }
¡¡ public void setIfselected(boolean ifselected) {
¡¡¡¡ this.ifselected = ifselected;
¡¡ }
¡¡ public String toString() {
¡¡¡¡ return this.label+" ";
¡¡ }
¡¡ public String getImagePath() {
¡¡¡¡ return imagePath;
¡¡ }
¡¡ public void setImagePath(String imagePath) {
¡¡¡¡ this.imagePath = imagePath;
¡¡ }
¡¡ public String getSelectImgPath() {
¡¡¡¡ return selectImgPath;
¡¡ }
¡¡ public void setSelectImgPath(String selectImgPath) {
¡¡¡¡ this.selectImgPath = selectImgPath;
¡¡ }
}

--------------------------------------------------------------------------------

package com.skystudio.ExpandList;

import java.util.Vector;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;

import com.skystudio.ui.toolkit.Util;

/**
* @author sky
*
*/
public class ExpandList extends List implements CommandListener {
¡¡ private Vector itemList = new Vector();

¡¡ private ExpandListItem currentSelectedObject = null;

¡¡ private int currentSelectedIndex = -1;

¡¡ private Vector appearHookList = new Vector();

¡¡ public ExpandList(String title, int type, Vector itemList) {
¡¡¡¡ super(title, type);
¡¡¡¡ this.itemList = itemList;
¡¡¡¡ this.setCommandListener(this);
¡¡¡¡ LoadList();
¡¡ }

¡¡ public void appendItem(ExpandListItem item, Image icon, boolean ifSub) {
¡¡¡¡ appearHookList.addElement(item);
¡¡¡¡ System.out.println("Add current display list:" + item);
¡¡¡¡ if (!ifSub) {
¡¡¡¡¡¡ this.append(item.getLabel(), icon);
¡¡¡¡ } else {
¡¡¡¡¡¡ this.append(" " + item.getLabel(), icon);
¡¡¡¡ }
¡¡ }

¡¡ public void Init() {
¡¡¡¡ int count = this.size();
¡¡¡¡ for (int i = 0; i < count; i++) {
¡¡¡¡¡¡ this.delete(0);
¡¡¡¡ }
¡¡¡¡ this.appearHookList.removeAllElements();
¡¡¡¡¡¡ System.out.println("Now itemList:" + this.itemList);
¡¡ }

¡¡ public void LoadList() {
¡¡¡¡ Init();
¡¡¡¡ for (int i = 0; i < itemList.size(); i++) {
¡¡¡¡¡¡ ExpandListItem elItem = (ExpandListItem) itemList.elementAt(i);
¡¡¡¡¡¡ if (elItem.getType() == ExpandListItem.GROUP) {
¡¡¡¡¡¡¡¡ Image icon = Util.getImage(elItem.getImagePath());
¡¡¡¡¡¡ /**
¡¡¡¡¡¡¡¡ * @Debug
¡¡¡¡¡¡ */
¡¡¡¡¡¡ if (elItem.Ifselected()) {
¡¡¡¡¡¡¡¡ if (elItem.getSelectImgPath() != null) {
¡¡¡¡¡¡¡¡¡¡ icon = Util.getImage(elItem.getSelectImgPath());
¡¡¡¡¡¡¡¡ }
¡¡¡¡¡¡¡¡ System.out.println("Add Parent Node:");
¡¡¡¡¡¡¡¡ this.appendItem(elItem, icon, false);
¡¡¡¡¡¡¡¡ Vector group = (Vector) elItem.getContent();
¡¡¡¡¡¡¡¡ for (int j = 0; j < group.size(); j++) {
¡¡¡¡¡¡¡¡¡¡ ExpandListItem item = (ExpandListItem) group.elementAt(j);
¡¡¡¡¡¡¡¡¡¡ Image ic = Util.getImage(item.getImagePath());
¡¡¡¡¡¡¡¡¡¡ System.out.println("Add Sub Node:");
¡¡¡¡¡¡¡¡¡¡ this.appendItem(item, ic, true);
¡¡¡¡¡¡¡¡ }
¡¡¡¡¡¡¡¡ } else {
¡¡¡¡¡¡¡¡¡¡ System.out.println("Add Leave Node:");
¡¡¡¡¡¡¡¡¡¡ this.appendItem(elItem, icon, false);
¡¡¡¡¡¡¡¡ }
¡¡¡¡¡¡ } else if (elItem.getType() == ExpandListItem.ITEM) {
¡¡¡¡¡¡¡¡ Image icon = Util.getImage(elItem.getImagePath());
¡¡¡¡¡¡¡¡ this.appendItem(elItem, icon, false);
¡¡¡¡¡¡ }
¡¡¡¡ }
¡¡¡¡ if (this.currentSelectedIndex != -1) {
¡¡¡¡¡¡ this.setSelectedIndex(currentSelectedIndex, true);
¡¡¡¡ }
¡¡ }

¡¡ public Vector getItemList() {
¡¡¡¡ return itemList;
¡¡ }

¡¡ public void setItemList(Vector itemList) {
¡¡¡¡ this.itemList = itemList;
¡¡ }

¡¡ public void commandAction(Command arg0, Displayable arg1) {
¡¡¡¡ if (arg0 == List.SELECT_COMMAND) {
¡¡¡¡¡¡ /**
¡¡¡¡¡¡¡¡ * Set Current List Selected status
¡¡¡¡¡¡ */
¡¡¡¡¡¡ this.currentSelectedIndex = this.getSelectedIndex();
¡¡¡¡¡¡ System.out.println(this.appearHookList);

¡¡¡¡¡¡ this.currentSelectedObject = (ExpandListItem) this.appearHookList.elementAt(currentSelectedIndex);

¡¡¡¡¡¡ int indexInItemList = this.itemList.indexOf(this.appearHookList.elementAt(this.getSelectedIndex()));
¡¡¡¡¡¡ System.out.println(" Selected: " + currentSelectedIndex + " " + this.currentSelectedObject + " indexInItemList:" + indexInItemList);
¡¡¡¡¡¡ /**
¡¡¡¡¡¡ *
¡¡¡¡¡¡ */
¡¡¡¡¡¡ if (this.currentSelectedObject.getType() == ExpandListItem.GROUP) {
¡¡¡¡¡¡¡¡ if (this.currentSelectedObject.Ifselected() == false) {// Previous
¡¡¡¡¡¡¡¡¡¡ // item
¡¡¡¡¡¡¡¡¡¡ // status
¡¡¡¡¡¡¡¡¡¡ // is
¡¡¡¡¡¡¡¡¡¡ // contractive,need
¡¡¡¡¡¡¡¡¡¡ // to be
¡¡¡¡¡¡¡¡¡¡ // expanded.
¡¡¡¡¡¡¡¡¡¡ System.out.println(this.currentSelectedObject.Ifselected());
¡¡¡¡¡¡¡¡¡¡ this.itemList.removeElementAt(indexInItemList);
¡¡¡¡¡¡¡¡¡¡ this.currentSelectedObject.setIfselected(true);
¡¡¡¡¡¡¡¡¡¡ this.itemList.insertElementAt(currentSelectedObject,
¡¡¡¡¡¡¡¡¡¡ indexInItemList);
¡¡¡¡¡¡¡¡ } else {
¡¡¡¡¡¡¡¡¡¡ this.itemList.removeElementAt(indexInItemList);
¡¡¡¡¡¡¡¡¡¡ this.currentSelectedObject.setIfselected(false);
¡¡¡¡¡¡¡¡¡¡ this.itemList.insertElementAt(currentSelectedObject,
¡¡¡¡¡¡¡¡¡¡ indexInItemList);
¡¡¡¡¡¡¡¡ }
¡¡¡¡¡¡¡¡ this.Init();
¡¡¡¡¡¡¡¡ this.LoadList();
¡¡¡¡¡¡ } else {
¡¡¡¡¡¡¡¡ if (this.currentSelectedObject.getSelectImgPath() != null) {
¡¡¡¡¡¡¡¡¡¡ if (this.currentSelectedObject.Ifselected() == false) {
¡¡¡¡¡¡¡¡¡¡¡¡ Image icon = Util.getImage(this.currentSelectedObject.getSelectImgPath());
¡¡¡¡¡¡¡¡¡¡¡¡ System.out.println(this.currentSelectedObject.Ifselected());
¡¡¡¡¡¡¡¡¡¡¡¡ this.itemList.removeElementAt(indexInItemList);
¡¡¡¡¡¡¡¡¡¡¡¡ this.currentSelectedObject.setIfselected(true);
¡¡¡¡¡¡¡¡¡¡¡¡ this.itemList.insertElementAt(currentSelectedObject,indexInItemList);
¡¡¡¡¡¡¡¡¡¡¡¡ this.delete(this.currentSelectedIndex);
¡¡¡¡¡¡¡¡¡¡¡¡ this.insert(this.currentSelectedIndex,
¡¡¡¡¡¡¡¡¡¡¡¡ this.currentSelectedObject.getLabel(), icon);
¡¡¡¡¡¡¡¡¡¡ } else {
¡¡¡¡¡¡¡¡¡¡¡¡ Image icon = Util.getImage(this.currentSelectedObject.getImagePath());
¡¡¡¡¡¡¡¡¡¡¡¡ this.itemList.removeElementAt(indexInItemList);
¡¡¡¡¡¡¡¡¡¡¡¡ this.currentSelectedObject.setIfselected(false);
¡¡¡¡¡¡¡¡¡¡¡¡ this.itemList.insertElementAt(currentSelectedObject,indexInItemList);
¡¡¡¡¡¡¡¡¡¡¡¡ this.delete(this.currentSelectedIndex);
¡¡¡¡¡¡¡¡¡¡¡¡ this.insert(this.currentSelectedIndex,
¡¡¡¡¡¡¡¡¡¡¡¡ this.currentSelectedObject.getLabel(), icon);
¡¡¡¡¡¡¡¡¡¡ }
¡¡¡¡¡¡¡¡¡¡ this.setSelectedIndex(this.currentSelectedIndex,true);
¡¡¡¡¡¡¡¡ }
¡¡¡¡¡¡ }
¡¡¡¡ }
¡¡ }
}
¡¡¡¡¸½²âÊÔ´úÂë

import java.util.Vector;

import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.skystudio.Canvas.ListCanvas;
import com.skystudio.ExpandList.ExpandList;
import com.skystudio.ExpandList.ExpandListItem;

public class Main extends MIDlet {
¡¡ Display d=null;
¡¡ protected void startApp() throws MIDletStateChangeException {
¡¡¡¡ d=Display.getDisplay(this);
¡¡¡¡ ListTest();
¡¡ }
¡¡ private void TestUI(){
¡¡¡¡ ListCanvas l=new ListCanvas();
¡¡¡¡ d.setCurrent(l);
¡¡ }
¡¡ private void ListTest(){
¡¡¡¡ Vector v1=new Vector();
¡¡¡¡ for(int i=0;i<10;i++){
¡¡¡¡¡¡ v1.addElement(new ExpandListItem("ÍÁ·Ë"+Integer.toString(i),"/img/default.png","/img/Group-open.png","ÍÁ·Ë"+Integer.toString(i),ExpandListItem.ITEM,false));
¡¡¡¡ }
¡¡¡¡ String v2="¾¯²ì";
¡¡¡¡ Vector v3=new Vector();
¡¡¡¡ for(int i=0;i<10;i++){
¡¡¡¡¡¡ v3.addElement(new ExpandListItem("¾¯²ì"+Integer.toString(i),"/img/default.png","/img/Group-open.png","¾¯²ì"+Integer.toString(i),ExpandListItem.ITEM,false));
¡¡¡¡ }
¡¡¡¡ Vector v=new Vector();
¡¡¡¡ v.addElement(new ExpandListItem(v1,"/img/Group-close.png","/img/Group-open.png","ÍÁ·Ë°ï",ExpandListItem.GROUP,false));
¡¡¡¡ v.addElement(new ExpandListItem(v3,"/img/Group-close.png","/img/Group-open.png","¾¯²ì¾Ö",ExpandListItem.GROUP,false));
¡¡¡¡ v.addElement(new ExpandListItem(v2,"/img/default.png","/img/Group-open.png","·¨¹Ù",ExpandListItem.ITEM,false));
¡¡¡¡ d.setCurrent(new ExpandList("»¨Ãû²á",Choice.IMPLICIT,v));
¡¡ }

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

¡¡ protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
¡¡¡¡ // TODO Auto-generated method stub
¡¡ }
}
µÚ 2 Ò³£¬¹² 2 Ò³ [1] [2]
Õ¾ÄÚËÑË÷