[英]Changing a background color of an item in Listview
我有一個列表視圖,其中有一個可見的單項。
lv.getChildAt(0).setBackgroundColor(Color.RED));
這總是返回NullPointerException。
我也試圖理解為什么在使用視圖時lv.getChildCount()返回-1。
嘗試listView.getAdapter()。getView(0,null,listView).setBackgroundColor(Color.RED);
嘗試更改適配器的背景色。 這是有關如何進行更改的示例。
public class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MySimpleArrayAdapter(Context context, String[] values) {
super(context, R.layout.rowlayout, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
textView.setText(values[position]);
**// change the background
if(position == 0)
rowView.setBackgroundColor(Color.RED);**
String s = values[position];
if (s.startsWith("iPhone")) {
imageView.setImageResource(R.drawable.no);
} else {
imageView.setImageResource(R.drawable.ok);
}
return rowView;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.