Lwuit List详解

在网上搜索有关于Lwuit之List详解,结果有很多是让我不满意的,基本上都把List讲解得很复杂..自己摸索了一下,其实也挺简单的...
最重要的是只要清楚list.setListCellRenderer(ListCellRenderer cellRenderer)的用法,在此我也不详说,大家自己看代码,我会在代码里加了点点注释...
 
1.Entity又叫实体层(Model),一个非常普通的的POJO类.
package com.lwuit.textarea.list2;

public class Person {
   private String id;
   private String name;
   public String getId() {
     return id;
  }
   public void setId(String id) {
     this.id = id;
  }
   public String getName() {
     return name;
  }
   public void setName(String name) {    
     this.name = name;
  }    
}
 
2.View层,主要负责视图的展示:
package com.lwuit.textarea.list2;

import java.util.Vector;

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.List;
import com.sun.lwuit.animations.CommonTransitions;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Resources;

public class ListTestMIDlet extends MIDlet{

   protected void startApp() throws MIDletStateChangeException {
    Display.init( this);    
                 try {
                            Resources r = Resources.open( "/resources.res");
                         UIManager.getInstance().setThemeProps(r.getTheme(
                                r.getThemeResourceNames()[0])
                                 );
                } catch (java.io.IOException e) {
                }                
                
                 //1. create Listmodel
                Vector vector = new Vector();                
                Person p1 = new Person();
                p1.setId( "001");                
                p1.setName( "张三");
                
                
                Person p2 = new Person();
                p2.setId( "002");
                p2.setName( "ZhangSan");
                
                Person p3 = new Person();
                p3.setId( "003");
                p3.setName( "LiSin");
                
                Person p4 = new Person();
                p4.setId( "004");
                p4.setName( "王五");
                
                vector.addElement(p1);
                vector.addElement(p2);
                vector.addElement(p3);
                vector.addElement(p4);
        
                
                
                 //2.add an to list
                List list = new List(vector);
                 //3.setListCellRenderer
                 list.setListCellRenderer(new MyPersonRenderer());     //重点在于此,创建一个渲染对象,负责对数据的展示
                
                list.addActionListener( new ActionListener(){
         public void actionPerformed(ActionEvent evt) {
          Person p = (Person)( (List)( evt.getSource() ) ).getSelectedItem();
          System.out.println(p.getId()+ " "+p.getName()+ " ");
        }         //中文此处会乱码,请不要在resources中设置font样式 

                });
                
                Form f = new Form();                
                f.setTitle( "List Test标题");
                f.setTransitionOutAnimator(CommonTransitions.createFade(400));
                f.setLayout( new BorderLayout());
                
                f.addComponent(BorderLayout.CENTER, list);                
                f.show();
  }
    
   protected void destroyApp( boolean arg0) throws MIDletStateChangeException {
    
  }

   protected void pauseApp() {
    
  }
    
}
 
3.View层,负责对数据进行渲染..
package com.lwuit.textarea.list2;

import com.sun.lwuit.Component;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.list.ListCellRenderer;

public class MyPersonRenderer extends Label implements ListCellRenderer{ 
     
  //List中的把每一个对象,循环传递给Object,此处的Object对表的是我们的PJO类.
   public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {
    Person p =(Person)value;    
     this.setText(p.getId()+ " "+p.getName());

     if(isSelected){   //如果为选中状态,字体前景颜色为红色
       this.getStyle().setFgColor(0xFF0000);
    } else{
       this.getStyle().setFgColor(0xFFFFFF);
    }
     return this;
  }

   public Component getListFocusComponent(List list) {
    Label label = new Label("");
    label.getStyle().setBgTransparency(100);
     return label;
  }

}
 
乱码问题:
   乱码问题困扰我一整晚上,后来经过我不断的测试, 从wuit1.3 到 Lwuit1.5都是这样的.
解决方法是:
   请不要在resources.res文件里设置font样式,否则,将不会显示中文,TextField 和 TextArea我测试却可以正常获得中文.
 
在整个里面我却没有提到Contorl层,其实,大家可以这样理解,控制层就是事件处理层...
运行后,效果如下图

你可能感兴趣的:(职场,乱码,休闲,LWUIT,List详解)