LWUIT 中list的使用

卖萌 2010-12-25 11:09:43
我用想用list显示一个这样的效果:list中,前面是一个图标,图标后面跟着几小列,如
就是中间的list ,前面是个小图标,后面的跟着两个小列。。。
我的ListCellRenderer 是这么写的,明明就添加了theme ,time ,email 三个,可是显示的时候图标后面却只有theme 一个,请教高手
class ContactsRenderer extends Container implements ListCellRenderer {

private Label theme = new Label("");
private Label time = new Label("");
private Label email = new Label("");
private Label pic = new Label("");

private Label focus = new Label("");

public ContactsRenderer() {
setLayout(new BorderLayout());
addComponent(BorderLayout.WEST, pic);
Container cnt = new Container(new BoxLayout(BoxLayout.Y_AXIS));
theme.getStyle().setBgTransparency(0);
time.getStyle().setBgTransparency(0);
email.getStyle().setBgTransparency(0);
pic.getStyle().setBgTransparency(0);

theme.getStyle().setFgColor(0xffffff);
time.getStyle().setFgColor(0xffffff);
email.getStyle().setFgColor(0xffffff);

cnt.addComponent(theme);
cnt.addComponent(email);
cnt.addComponent(time);

addComponent(BorderLayout.CENTER, cnt);
focus.setFocus(true);

}

public Component getListCellRendererComponent(List list, Object value,
int index, boolean isSelected) {
return this;
}

public Component getListFocusComponent(List list) {
return focus;
}
}
...全文
72 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Gucci 2011-01-05
  • 打赏
  • 举报
回复
看下面的例子,我也是这么做的
private String globalstrURL = "";

public void initform() {
removeAll();
removeAllCommands();
String[][] strAccessories = EsionData.getInstance()
.GetSiteList("05342");

if (strAccessories != null) {
SiteListInfo[] accessoryinfoarray = new SiteListInfo[strAccessories.length];
Image img = getRes().getImage("YdFj.gif");
for (int i = 0; i < strAccessories.length; i++) {
accessoryinfoarray[i] = new SiteListInfo(strAccessories[i][0],
strAccessories[i][1], img);
}
// 设置使得list的选中状态的层能够自适应的足够撑开
setLayout(new BorderLayout());
setScrollable(false);
//创建List并传入相应的数据
addComponent(BorderLayout.CENTER, createList(accessoryinfoarray, List.VERTICAL, new ContactsRenderer()));
} else
addComponent(new Label("网络出现异常,没有找到任何资讯站点!"));
}

// List初始化
private List createList(final SiteListInfo[] SiteLists, int orientation,
ListCellRenderer renderer) {
final List list = new List(SiteLists);
list.getStyle().setBgTransparency(0);
list.setListCellRenderer(renderer);
list.setOrientation(orientation);
return list;
}

// list数据绑定
class ContactsRenderer extends Container implements ListCellRenderer {
private Label name = new Label("");
private Label pic = new Label("");
private Label focus = new Label("");

public ContactsRenderer() {
setLayout(new BorderLayout());
//设置list每一列的图标
addComponent(BorderLayout.WEST, pic);
Container cnt = new Container(new BoxLayout(BoxLayout.Y_AXIS));
name.getStyle().setBgTransparency(0);
name.getStyle().setFont(
Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,
Font.SIZE_MEDIUM));
//设置list每一列内容
cnt.addComponent(name);
addComponent(BorderLayout.CENTER, cnt);
// 这里设置选中颜色,也可不设置通过Lwuit的资源管理器设置选中颜色也可
// 不过不能跟底色重合,否则看不见效果,其实这也是相对于多普达的手机才需要这样设置
focus.getStyle().setBgColor(0xffffff);
focus.getStyle().setBgTransparency(100);
}

public Component getListCellRendererComponent(List list, Object value,
int index, boolean isSelected) {
SiteListInfo sitelist = (SiteListInfo) value;
name.setText(sitelist.getsitename());
pic.setIcon(sitelist.getpic());
return this;
}

public Component getListFocusComponent(final List list) {
return focus;
}
}
//创建一个类用来放置List中需要的数据,类似的如需要传值到另一个窗体,或者list中列显示的数据
class SiteListInfo {
private String sitename;
private String siteurl;
private Image pic;

public SiteListInfo(String sitename, String siteurl, Image pic) {
this.sitename = sitename;
this.siteurl = siteurl;
this.pic = pic;
}

public String getsitename() {
return sitename;
}

public String getsiteurl() {
return siteurl;
}

public Image getpic() {
return pic;
}
}

13,100

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧