所以我一直在修改记事本教程代码:http://developer.android.com/resources/tutorials/notepad/notepad-ex1.html。
基本上,我想做的是根据内容为ListView中的不同行创建特定的布局样式(即背景颜色)。例如,标题的文本是"1“,所以该行的背景将是红色的。或者,如果标题的文本是"2“,那么该行(或列表项)的背景将是绿色的。
我想要完成的是对数据库中的行进行汇总,并根据项目存储的类别(数值字段)对每行进行颜色编码。
发布于 2011-08-12 13:35:22
它们使用SimpleCursorAdapter,您需要自己编写才能更改输出。在处理数据表示的适配器中,可以覆盖getView()方法。然后用来自数据库的数据填充每一行。根据数据,设置不同的背景色。
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.my_layout, null);
}
Cursor.moveToPosition(position);
ImageView img = (ImageView)v.findViewById(R.id.bg);
if (Cursor.getString(Cursor.getColumnIndex("column")).equals("black")) {
img.setImageBackground(Color.Black);
}
return v;发布于 2011-08-12 15:20:24
华荣的答案是正确的。您可能还希望使用getItem方法来获取列表中特定位置的项,以确定它是什么类型的数据。然后在getView方法中更改该行的背景色
https://stackoverflow.com/questions/7035866
复制相似问题