简体   繁体   English

ListView setBackgroundResource无法正常工作

[英]ListView setBackgroundResource not working android

I have problem wit changing backgroud color in my project. 我在更改项目中的背景颜色时遇到问题。 I'm just trying to change list background color with condition in ViewBinder. 我只是想用ViewBinder中的条件更改列表背景颜色。 But color of backgroung didn't changing. 但是背景的颜色没有改变。

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

I think, problem is in argument of setBackgroundResource(R.color.votedColor). 我认为问题出在setBackgroundResource(R.color.votedColor)的参数上。 Argument should be a resource id for example some R.drawable.votedColor. 参数应为资源ID,例如一些R.drawable.votedColor。 Take a look setBackgroundResource and to make a drawable xml file take a look at Drawable Resource . 看一下setBackgroundResource并制作一个可绘制的xml文件,看看Drawable Resource

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM