ListView setBackgroundResource无法运行android

时间:2013-01-26 14:38:28

标签: java android background android-viewbinder

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

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

的onCreate

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" />

1 个答案:

答案 0 :(得分:0)

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