怎样把ListView每个条目显示不同的颜色?

fslkd5h fslkd5h 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我用的是SimpleAdapter适配器,
SimpleAdapter simpleAdapter=new SimpleAdapter(context, data, resource, from, to);
我是这样写的:

List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
Map<String,Object> map=new HashMap<String,Object>();
map.put("item1", "item1 content");
map.put("item2", "item2 content");
map.put("item3", "item3 content");
list.add(map);

SimpleAdapter simpleAdapter=new MyAdapter(this, list,R.layout.linearLayout1, new String[]{"item1","item2","item3"}, new int[]{R.id.TextView1,R.id.TextView2,R.id.TextView3});

为什么 resource 属性里,既可以写成我自己写的、包含三个TextView的布局文件,还可以写成Android默认的
布局文件android.R.layout.simple_list_item_1,有什么不同?

为了实现我的目的,我写了个类继承SimpleAdapter、重载getView()方法:
@Override
public View getView(int position, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
View v= getView(position, arg1, arg2);
if (v instanceof TextView) {
if (position%2==0) {
v.setBackgroundColor(Color.BLUE);
} else {
v.setBackgroundColor(Color.RED);
}
}
return v;
}
效果: 三个条目颜色没有改变

我又把 resource 属性改成 android.R.layout.simple_list_item_1 效果:只剩下一个蓝色条目,且没有颜色

有人说:继承BaseAdapter,我不知道怎么写?

咋整?
...全文

相关推荐

评论 10
收藏 已收藏
分享
返回顶部
fslkd5h
Android 80,490 成员 91,375 内容