[英]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.