本文介绍了使用ViewBinder与TextView的自定义ListView项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我试图使用<$ C $在我的的ListView 添加背景颜色为的TextView C> SimpleCursorAdapter 与 ViewBinder ,但它不工作:

I'm trying to add a background color for an TextView on my ListView using a SimpleCursorAdapter with ViewBinder, but it doesn't work:

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;
    }
}

我在做什么错了?

What am I doing wrong?

推荐答案

setViewValue(),你一旦你设置的值要为返回true指定视图。退房的更多信息文档。这里就是我想为您的code:

In setViewValue(), you have to return true once you've set the value wanted for the specified view. Check out the documentation for more info. Here's what I'm thinking of for your code:

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;
}

这篇关于使用ViewBinder与TextView的自定义ListView项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 19:09