使用ViewBinder与TextView的自定义ListView项 [英] Using ViewBinder for a custom ListView item with TextView

查看:260
本文介绍了使用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指定视图。退房的<一个href=\"http://developer.android.com/reference/android/widget/SimpleCursorAdapter.ViewBinder.html#setViewValue%28android.view.View,%20android.database.Cursor,%20int%29\"相对=nofollow>更多信息文档。这里就是我想为您的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项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆