SimpleCursorAdapter.ViewBinder - 对每一行应用不同(ListFragment)

时间:2013-06-04 17:45:59

标签: java android simplecursoradapter android-listfragment

在我的应用程序中,我从SQlite数据库中提取3个字符串以填充ListView。在listrow中,我必须使用TextViews和ImageView。

当前正在填充两个TextView(每行中包含不同的文本)。但是,我使用ViewBinder设置的ImageView不是这种情况。即,每行中的图像都具有相同的颜色。我怎样才能解决这个问题?我需要扩展SimpleCursorAdapter吗?

String[] from = {DbHelper.NAME, DbHelper.COMMENT, DbHelper.COLOR};
String[] column = {DbHelper.C_ID, DbHelper.NAME, DbHelper.COMMENT, DbHelper.COLOR};
int[] to = {R.id.listitem_title, R.id.listitem_summary, R.id.listitem_color};

Cursor cursor = db.query(DbHelper.TABLE_NAME, column, null, null, null, null, null);

this.adapter = new SimpleCursorAdapter(getActivity(), R.layout.list_item_set, cursor, from, to);

adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
    if (view.getId() == R.id.listitem_color) {

        cursor.moveToFirst();
        String color = "#ffffff";
        while (cursor.moveToNext()) {
        color = cursor.getString(cursor.getColumnIndex(DbHelper.COLOR));
        }
        ((ImageView) view).setBackgroundColor(Color.parseColor(color));
            return true;
        }
        return false;
         }
    });

setListAdapter(adapter);

1 个答案:

答案 0 :(得分:1)

  

即,每行中的图像都具有相同的颜色。我该怎么办?   此?

如果您打算使用:

String color = "#ffffff";
while (cursor.moveToNext()) {
    color = cursor.getString(cursor.getColumnIndex(DbHelper.COLOR));
}

当您遍历整个color时,这会使Cursor指向Cursor最后一行中的颜色。相反,您只想从color获取Cursor

String color = "#ffffff";
color = cursor.getString(cursor.getColumnIndex(DbHelper.COLOR));