Android ListView适配器“;“填写”;回拨
我使用SimpleAdapter填充ListView。在这样做之后,我有一个函数,它尝试在子视图上循环,以编程方式设置它们的背景色。问题是调用ListView.setAdapter()后,ListView可能没有任何子级。我不知道我的函数应该放在哪个回调函数上Android ListView适配器“;“填写”;回拨,android,Android,我使用SimpleAdapter填充ListView。在这样做之后,我有一个函数,它尝试在子视图上循环,以编程方式设置它们的背景色。问题是调用ListView.setAdapter()后,ListView可能没有任何子级。我不知道我的函数应该放在哪个回调函数上 // A HashMap to store the values for the ListView rows List<HashMap<String, String>> aList = ne
// A HashMap to store the values for the ListView rows
List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < steps.length; i++) {
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("txt", steps[i]);
hm.put("icon", Integer.toString(step_images[i]));
aList.add(hm);
}
// Keys used in Hashmap
String[] from = {"icon", "txt"};
// Ids of views in layout
int[] to = {R.id.icon, R.id.txt};
// Instantiating an adapter to store each items
// R.layout.listview_steps defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_steps, from, to);
// Setting the adapter to the listView
listView.setAdapter(adapter);
//fixme after the listView adapter is set, the listView may not have any children immediately.
//I'm not sure which callback to use to properly call refreshStepsListView().. So I'm hacking it and I just wait 250ms
(new Handler())
.postDelayed(
new Runnable() {
public void run() {
refreshStepsListView();
}
}, 250);
//用于存储ListView行值的HashMap
列表列表=新的ArrayList();
对于(int i=0;i
不需要回调。现场直播吗
public class ColoredAdapter extends SimpleAdapter {
ColoredAdapter(...) {
super(context, list, reslayout, from, to);
}
getView(...) {
// set colors here
}
或者您可以创建一个
步骤
类的列表,而不是Hashmap,还可以使用阵列适配器
在适配器内部进行背景着色。您只需扩展simpledapter
,即可在创建视图时访问它们。在适配器中,您需要执行任何视图操作逻辑
不建议对子项进行迭代,这将导致错误、无法维护的代码和糟糕的性能
public class ColorAdapter extends SimpleAdapter {
public ColorAdapter(Context context,
List<? extends Map<String, ?>> data,
int resource,
String[] from,
int[] to) {
super(context, data, resource, from, to);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
view.setBackgroundColor(Color.RED);
return view;
}
}
公共类ColorAdapter扩展了SimpleAdapter{
公共颜色适配器(上下文,
列表>数据,
int资源,
字符串[]来自,
int[]至){
超级(上下文、数据、资源、从、到);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图=super.getView(位置、转换视图、父级);
视图.setBackgroundColor(颜色.红色);
返回视图;
}
}
据我所知。您只需要更改listview中每个项目的背景
首先,为适配器定义类项
public class Item {
private String mTitle;
private String mBackgroundColor;
public void setTittle(String title){
this.mTitle = title;
}
public void setBackgroundColor(int color){
this.mBackgroundColor= color;
}
public void getTitle (){
return this.mTitle;
}
public void getBackgroundColor(){
return this.mBackgroundColor;
}
}
然后使用ArrayAdapter
而不是simpledapter
看到这个样本了吗
确保在ArrayAdapter
的getView()
中,您已从item.getBackgroundColor()设置了项目背景色
当您想要更改背景颜色时,您只需将新颜色设置为哪个项item.setBackgroundColor(newColor)
并调用adapter.notifyDataSetChanged()
刷新适配器。您需要在适配器上实现getView!这应该行得通。在另一个模型中,颜色由州标志驱动,但我会解决这个问题。我不熟悉安卓系统。总的来说,我需要花更多的时间使用适配器。我基本上就是这样做的,而且效果很好。很高兴听到这个消息:)
itemView.setBackgroundResource(item.getBackgroundColor());