【问题标题】:Using ViewBinder for a custom ListView item with TextView将 ViewBinder 用于带有 TextView 的自定义 ListView 项
【发布时间】:2011-10-02 19:57:46
【问题描述】:

我正在尝试使用 SimpleCursorAdapterViewBinder 在我的 ListView 上添加 TextView 的背景颜色,但它不起作用:

listrow.xml:

<TextView
    android:id="@+id/catcolor"
    android:layout_width="4dp"
    android:layout_height="match_parent"
    android:layout_margin="4dp"
    android:background="#FF0099FF" />

    <LinearLayout
        android:paddingTop="4dp"
        android:paddingRight="4dp"
        android:paddingLeft="4dp"
        android:paddingBottom="2dp">

        <TextView
            android:id="@+id/title"
            android:text="{title}"
            android:textSize="@dimen/text_size_medium"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="10"
            android:textStyle="bold" />

    </LinearLayout>

我称之为ViewBinder的活动(类TextActivity 扩展 ListActivity

private void fillData() {
    Cursor notesCursor = mDbHelper.fetchAllNotes();
    startManagingCursor(notesCursor);

    String[] from = new String[]{ NoteAdapter.KEY_CATID, NoteAdapter.KEY_TITLE, NoteAdapter.KEY_CONTENT };
    int[] to = new int[]{ R.id.catcolor, R.id.title, R.id.content };

    SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.noterow, notesCursor, from, to);
    notes.setViewBinder(new ViewBinder());
    setListAdapter(notes);
}

private class ViewBinder implements SimpleCursorAdapter.ViewBinder {
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        int viewId = view.getId();
        switch(viewId) {
        case R.id.catcolor:
            ImageView catcolor = (ImageView) view;

            int catColor = cursor.getInt(columnIndex);
            switch(catColor) {
            case 0:
                catcolor.setBackgroundColor(R.color.catcolor1);
            break;
            case 1:
                catcolor.setBackgroundColor(R.color.catcolor2);
            break;
            case 2:
                catcolor.setBackgroundColor(R.color.catcolor3);
            break;
            }
        break;
        }
        return false;
    }
}

我做错了什么?

【问题讨论】:

    标签: android android-listview textview simplecursoradapter android-viewbinder


    【解决方案1】:

    setViewValue() 中,一旦为指定视图设置了所需的值,就必须返回true。查看documentation 了解更多信息。这是我对您的代码的想法:

    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        int viewId = view.getId();
        switch(viewId) {
        case R.id.catcolor:
            ImageView catcolor = (ImageView) view;
    
            int catColor = cursor.getInt(columnIndex);
            switch(catColor) {
            case 0:
                catcolor.setBackgroundColor(R.color.catcolor1);
                return true;
            break;
            case 1:
                catcolor.setBackgroundColor(R.color.catcolor2);
                return true;
            break;
            case 2:
                catcolor.setBackgroundColor(R.color.catcolor3);
                return true;
            break;
            }
        break;
        }
        return false;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-28
      • 1970-01-01
      • 2023-03-23
      • 2017-06-09
      相关资源
      最近更新 更多