【问题标题】:ListView setBackgroundResource not working androidListView setBackgroundResource 无法正常工作
【发布时间】:2013-01-10 08:51:01
【问题描述】:

我在项目中更改背景颜色时遇到问题。我只是想用 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" />

【问题讨论】:

    标签: java android background android-viewbinder


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-22
      • 2017-02-09
      • 2015-07-27
      相关资源
      最近更新 更多