电信主站 网通分站
购买流程 付款方式 常见问题 在线提问 续租服务 购物车
用户名: 密 码: 忘记密码?
首 页
域名注册
虚拟主机
双线主机
服务器租用
VPS主机
企业邮局
代理专区
客服中心
虚拟主机行业资讯 虚拟主机评测对比 互联网最新动态 技术学院 站长资讯 在线教程 网站运营
搜索优化 服务器 网络编程 图形图象 站长之家 网页制作 操作系统
冲浪宝典 软件教学 视频通信 办公软件 邮件系统 网络安全 认证考试
您当前位置:西部数码->资讯中心-> 在线教程-> JSP教程
J2ME 实现可伸展目录树TreeList-JSP教程,J2ME开发
作者:网友供稿 点击:19
  西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!虚拟主机可在线rar解压,自动数据恢复设置虚拟目录等.虚拟主机免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金!
文章页数:[1] 
j2me里面有自带的list类,但是功能太弱,没有实现view和model的分离,所以操作起来比较费事。本来事想写一个canvas的treelist,但是画起来算坐标又太麻烦,所以选取了一个折中的方法,继承list,实现一个操作起来比较方便的组件。
       目的:
       1.可伸缩的目录树结构,暂时先实现两层。
       2.label和存储内容分离。
       3.激活和非激活图片分开。
       4.通过选择事件可以准确快速找到对应内容
       5.存储内容无关性,里面可以放置任何object
       实现思路:
       1.封装一个expanditem类,用来存储每一条数据。
/**
  * 默认图片
  */
 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;
 /**
  * 存储的对象
  */
          group表示这个item是一个父节点,下面包含字节点,这样它的content将是一个vector.
          item表示这个item是根节点。
         selectimgpath,是激活后的图标,可以为空,为空的时候选择了这个item图标不变。
然后就是expandlist类,此类的数据结构如下:
          private vector itemlist = new vector();
          /*用来存储内容的数据结构*/
           private expandlistitem currentselectedobject = null;
           /*当前所选择的对象,方便获取*/
           private int currentselectedindex = -1;
           /*当前选择的对象在队列中的index,队列有两个,一个是真实数据的存储vector,另外一个是显示在屏幕上的队列。这两个有时候是不一样的。因为有的节点有子节点*/
           private vector appearhooklist = new vector();
           /*显示在屏幕上的label队列*/
          
          总的思路如下:
           初始化list的时候,参数是一个vector,里面可以是expanditem或者是vector.然后根据expanditem里面的参数初始化屏幕,如果group节点的ifselected状态为true则递归添加下面的子节点,否则只插入当前节点。图标也是一样,如果ifselected为true 则用激活图标否则用默认图标。
          在用户选择了一个结点后,取得当前的激活的index号码,判断是不是父节点,如果是的话,首先更新这个父节点的ifselected属性为true,然后重画这个list;(其实效率更高的方法是直接插入这个父节点的子节点,但是这样做的话,在移除的时候会稍微稍微麻烦一点。有时间我在改过来,呵呵)。如果选择的是子节点,则判断是否有激活图标,如果有,则更新这个图标,就好了。
          下面是效果
附代码一份,这是我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
 }
}

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!
相关主题
文章页数:[1] 
Google
热门文章
·IReport与JasperReport开发详解一-JSP教程,Java技巧及代码
·JSTL(JSP标准标签库)介绍-JSP教程,资料/其它
·格式化输入日期时间控件 JieFormattedDateBox-JSP教程,Java技巧及代码
·JSTL简化JSP编码-JSP教程,Jsp/Servlet
·JUnit和单元测试入门简介-JSP教程,Java技巧及代码
·从数据库中读取一个图片并保存为一个图片文件-JSP教程,数据库相关
·Java Swing入门基础-JSP教程,Java技巧及代码
·IntelliJ IDEA培训-JSP教程,Java技巧及代码
·通过JSP的预编译消除性能瓶颈-JSP教程,Jsp/Servlet
·Java中利用JMF编写摄像头拍照程序-JSP教程,Java技巧及代码

最新文章
·J2ME中的时间处理方法
·J2ME播放声音流程简介
·J2ME开发之手机键盘使用注意问题
·J2ME 记录管理存储
·MVC模式在j2me项目中的应用(二)
·MVC模式在j2me项目中的应用(一)
·J2ME中使用记录存储系统(RMS)存储信息
·J2ME与MIDP开发(1)
·用简单的J2ME程序测试MIDlet的生命周期
·JAVA基础:提升JSP应用程序的七大绝招


 
 


版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!

特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
  打印  刷新  关闭
返回首页 |关于我们 | 联系我们 | 付款方式 | 创业联盟 | 虚拟主机 | 资讯中心 | 友情链接 | 网站地图

版权所有 西部数码(www.west263.com)
CopyRight (c) 2002~2006 west263.com all right reserved.
公司地址:四川成都市万和路90号天象大厦4楼 邮编:610031
电话总机:028-86262244 86263048 86263408 86263960 86264018 86267838
售前咨询:总机转201 202 203 204 206 208
售后服务:总机转211 212 213 214
财务咨询:总机转224 223 传真:028-86264041 财务QQ:点击发送消息给对方635483282
售前咨询QQ:点击发送消息给对方2182518 点击发送消息给对方241975952 点击发送消息给对方275026793 点击发送消息给对方408235859
售后服务QQ:点击发送消息给对方17708515 点击发送消息给对方307742704 点击发送消息给对方287976517 点击发送消息给对方363783715
《中华人民共和国增值电信业务经营许可证》编号:川B2-20030065号