0

我在项目中更改背景颜色时遇到问题。我只是想用 ViewBinder 中的条件更改列表背景颜色。但是背景的颜色没有改变。

我的视图绑定器

public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
LinearLayout ll = (LinearLayout) findViewById(R.id.listViewTopLinearLayout);
String voted = cursor.getString(VOTED_COLLUMN_INDEX);

    if (columnIndex == cursor.getColumnIndex(AssetsTableHelper.COLUMN_VOTED)) {
        boolean is_checked = voted.equals("true");
        if (is_checked) {
            ((View) ll.getParent()).setBackgroundResource(R.color.votedColor);
                ((View) view.getParent()).setBackgroundResource(R.color.votedColor);
        } else {
            ((View) view.getParent()).setBackgroundResource(R.color.notVotedColor);
        }
        return true;
    }
    return false;
};

创建

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
integrator = new IntentIntegrator(this);
datasource = new AssetsDataSource(this);
Cursor cursor = datasource.getCursorForAllAssets();
adapter = new SimpleCursorAdapter(this, R.layout.listview_item_row,cursor, UI_BINDING_FROM,UI_BINDING_TO, 0);
adapter.setViewBinder(new CustomViewBinder());
setListAdapter(adapter);

List_item_row.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/tvSurname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:textColor="#000000"
        android:textSize="20sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:textColor="#000000"
        android:layout_marginLeft="5dp"
        android:textSize="20sp"
        android:textStyle="bold" />
</LinearLayout>

<TextView
    android:id="@+id/tvHomeNumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:textColor="#000000"
    android:textSize="15sp"
    android:textStyle="bold" />
4

1 回答 1

0

我认为,问题在于 setBackgroundResource(R.color.votedColor) 的参数。参数应该是资源 id,例如一些 R.drawable.votedColor。看看setBackgroundResource并制作一个可绘制的 xml 文件,看看Drawable Resource

于 2013-10-12T09:41:13.160 回答