根据SimpleCursorAdapter字段更改ListView项目的背景颜色?

时间:2011-02-10 01:15:48

标签: android android-listview simplecursoradapter

我有一个使用RelativeLayout的自定义ListView项,看起来像这样......

| Time | Title        |
|      | Description  |

注意:左侧的“时间”是一个单独的全高TextView,其中“标题”和“描述为个人”堆叠'TextViews。

ListView项目表示使用SimpleCursorAdapter从SQLite DB中提取的电视节目。这很好但下一步是指示程序何时被安排记录(该应用程序是基于PC的PVR应用程序的Android客户端)。

SQL查询选择'start_time,title,description,is_set_to_record',前三个是要绑定到TextViews的文本字段,最后一个是布尔值。

似乎我可能需要扩展SimpleCursorAdapter,所以如果'is_set_to_record'为真,那么我将所有三个TextView的背景颜色设置为不同的颜色。问题是我不知道我应该在哪里做这件事。

这可能吗?如果是这样的话,任何指向我应该看的地方都会受到赞赏。

1 个答案:

答案 0 :(得分:4)

在扩展的SimpleCursorAdapter中,您可以执行以下操作:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
     View v = super.getView(position, convertView, parent);
     Cursor c = getCursor();
     c.moveToPosition(position);
     int col = c.getColumnIndex(is_set_to_record);
     boolean isSet = c.getInt(col) == 1;
     if (isSet) {
         // Set the background color of the text.
     } else {
         // Set the background color to something else.
     }
     return v;
}
相关问题