我想根据光标当前成员的某些属性来改变 ListItem 背景的颜色。我正在使用 SimpleCursorAdapter 将数据库的值绑定到 ListView。我所做的是创建一个这样的颜色选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="@android:color/holo_blue_dark" />
<item android:state_activated="true" android:state_selected="true" android:drawable="@android:color/holo_blue_dark" />
<item android:state_selected="true" android:drawable="@color/rosa" />
<item android:drawable="@android:color/transparent" />
</selector>
我的布局是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/listitem_color" >
...
...
我有一个视图活页夹,我在其中执行以下操作:
mAdapter = new SimpleCursorAdapter(getActivity(), R.layout.listitem, cur, cols, to,0);
mAdapter.setViewBinder(new ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (columnIndex == 6) {
boolean dudoso = cursor.getInt(columnIndex) == 1;
TextView textView = (TextView) view;
textView.setText(dudoso ? "dudoso" : null);
if(dudoso)
{
LinearLayout ll=((LinearLayout)textView.getParent().getParent());
ll.setSelected(true);
}
return true;
}
return false;
}
});
但它不起作用。做我想做的事的正确方法是什么?