Extensible directory tree Treelist in J2ME

Source: Internet
Author: User
Tags add object empty insert integer string stub tostring
J2ME has its own list class, but the function is too weak to realize the separation of view and model, so the operation is more trouble. Originally wanted to write a canvas of treelist, but the picture to calculate the coordinates is too troublesome, so chose a compromise method, inherit list, realize a more convenient operation of components.

Objective:

1. Scalable directory tree structure, the first two-tier implementation.

2.Label and storage content separation.

3. Activation and non-active pictures are separated.

4. To find the content accurately and quickly by selecting events

5. Storage content independent, inside can place any object

Realize the idea:

1. Encapsulates a Expanditem class for storing each piece of data.

/**
* 默认图片
*/
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 indicates that the item is a parent node, which contains a byte point so that its content will be a Vector. Item indicates that this item is the root node.

Selectimgpath, is the activation of the icon, you can be empty, empty when the choice of the item icon unchanged.

Then there is the Expandlist class, which has the following data structure:

private Vector itemList = new Vector();

/*用来存储内容的数据结构*/

private ExpandListItem currentSelectedObject = null;

/*当前所选择的对象,方便获取*/

private int currentSelectedIndex = -1;

/*当前选择的对象在队列中的Index,队列有两个,一个是真实数据的存储Vector,另外一个是显示在屏幕上的队列。这两个有时候是不一样的。因为有的节点有子节点*/

private Vector appearHookList = new Vector();

/*显示在屏幕上的Label队列*/

The general idea is as follows:

When initializing the list, the parameter is a vector, which can be either expanditem or vector. Then initialize the screen according to the parameters inside the Expanditem, if the group node's ifselected state is true recursionAdd the following child nodes, otherwise insert only the current node. The icon is the same, if ifselected is true, use the Activate icon otherwise the default icon.

After the user selects a node, get the current active index number, determine whether the parent node, if so, first update the parent node's Ifselected property is true, and then redraw the list; (in fact, the more efficient method is to directly insert the child node of this parent node, But doing so would be a little bit more difficult to remove. I have time to change over, hehe. If you select a child node, then determine if there is an activation icon, and if so, update the icon.

Here is the effect


A copy of the code, which is a very early version of my Me component library. Other components are written later. In fact, the best way is to write 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;
}
/**
* Default picture
*/
Private String imagepath= "";
/*
* Activate picture, if empty to indicate that this picture is invalid
*/
Private String Selectimgpath=null;
/**
* Group
*/
public static int group=1;
/**
* Record
*/
public static int item=0;
/**
* is selected
*/
Private Boolean ifselected=false;
/**
* Display Label
*/
Private String label;
/**
* Type: Group, record
*/
private int type;
/**
* Stored objects
*/
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;

Importjavax. 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;

/**
* @authorSky
*
*/
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:" + indexin ItemList);
/**
*
*/
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);
}
}
}
}
}

Attach test code

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 ("Bandit" +integer.tostring (i), "/img/default.png", "/img/group-open.png", "bandits" + Integer.tostring (i), expandlistitem.item,false));
}
String v2= "police";
Vector v3=new vector ();
for (int i=0;i<10;i++) {
V3.addelement (New Expandlistitem ("Police" +integer.tostring (i), "/img/default.png", "/img/group-open.png", "police" + Integer.tostring (i), expandlistitem.item,false));
}
Vector v=new vector ();
V.addelement (New Expandlistitem (v1, "/img/group-close.png", "/img/group-open.png", "Bandit Gang", Expandlistitem.group, false));
V.addelement (New Expandlistitem (v3, "/img/group-close.png", "/img/group-open.png", "Police Station", Expandlistitem.group, false));
V.addelement (New Expandlistitem (v2, "/img/default.png", "/img/group-open.png", "Judge", Expandlistitem.item,false));
D.setcurrent (New Expandlist ("roster", choice.implicit,v));
}

protected void Pauseapp () {
TODO auto-generated Method Stub
}

protected void Destroyapp (Boolean arg0) throws Midletstatechangeexception {
TODO auto-generated Method Stub
}
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.