我创建了一个 ArrayList<HashMap<String, String>>
为 ListView
保存我的数据的集合.我正在使用 SimpleAdapter
.
当列表项的 ID % 10 == 0 时,是否可以更改列表项的背景?
这是代码(生成布局的方法):
private void fillData() {
Cursor c = this.mDbManager.getNgOrderDetailByOrderNumber(this.mNumber);
ArrayList<HashMap<String, String>> items = new ArrayList<HashMap<String, String>>();
if (!c.isAfterLast()) {
do {
// ... filling HashMap and putting it to ArrayList
} while (c.moveToNext());
}
SimpleAdapter adapter = new SimpleAdapter(this, items, R.layout.list_item,
new String[] { "product", "ordered", "price", "discount" },
new int[] { R.id.ProductTextView, R.id.OrderedTextView,
R.id.PriceTextView, R.id.DiscountTextView });
ListView l = (ListView) findViewById(android.R.id.list);
l.setAdapter(adapter);
}
最佳答案
您覆盖 getView在您的适配器中更改 View 。请记住,ListView 会重用 View 实现,因此如果您将颜色更改为第 10 项,请确保将所有其他 View 的颜色设置为相反的颜色。
例如
new SimpleAdapter( ... ) {
@Override
public View getView (int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (position == 10) {
// set background color = red;
} else {
// set background color = green;
}
return view;
}
}
关于android - 如何自定义带有彩色项目背景的ListView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4199795/