我希望根据游标当前成员的某些属性来设置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;
}
});
但它不起作用。做我想要的正确方法是什么?
答案 0 :(得分:1)
也许更好的方法是编写自己的继承自SimpleCursorAdapter的类。这样的事情:
public class BubbleListAdapter extends SimpleCursorAdapter {
private LayoutInflater mLayoutInflater;
/** list holding statuses determining if an item is checked/unchecked. */
protected List<Boolean> mItemsCheckedStatus;
/** Cursor. */
private Cursor cursor;
/** reference to ArdoiseDatabaseAdapter. */
private MyDatabaseAdapter mMyDatabaseAdapter;
/** application context. */
private Context context;
/** references to Users. */
private Users mUsers;
/**
* View holder for color item.
*/
private static class ViewHolder {
/** bubble text. */
TextView buubleText;
/** user name. */
TextView userName;
}
public BubbleListAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to) {
super(context, layout, cursor, from, to);
this.cursor = cursor;
mLayoutInflater = LayoutInflater.from(context);
mMyDatabaseAdapter = MyDatabaseAdapter.getInstance();
this.context = context;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = mLayoutInflater.inflate(R.layout.bubble_list_item, null);
final ViewHolder holder = new ViewHolder();
holder.buubleText = (TextView) view.findViewById(R.id.bubble_list_text);
holder.userName = (TextView) view.findViewById(R.id.bubble_list_user_name);
view.setTag(holder);
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
Cursor c = getCursor();
final ViewHolder holder;
if (view != null) {
holder = (ViewHolder) view.getTag();
holder.buubleText.setText(c.getString(c.getColumnIndex(BubblesTableMetaData.BUBBLE_TEXT)));
if (mUsers != null) {
switch (c.getInt(c.getColumnIndex(BubblesTableMetaData.USER_ID))) {
case MyConstans.COLOR_RED:
holder.buubleText.setBackgroundResource(R.drawable.bubble_red_edit);
holder.userName.setTextColor(context.getResources().getColor(R.color.bubble_red));
holder.userName.setText(mUsers.getUser1());
break;
case MyConstans.COLOR_BLUE:
holder.buubleText.setBackgroundResource(R.drawable.bubble_blue_edit);
holder.userName.setTextColor(context.getResources().getColor(R.color.bubble_blue));
holder.userName.setText(mUsers.getUser2());
break;
case MyConstans.COLOR_GREEN:
holder.buubleText.setBackgroundResource(R.drawable.bubble_green_edit);
holder.userName.setTextColor(context.getResources().getColor(R.color.bubble_green));
holder.userName.setText(mUsers.getUser3());
break;
case MyConstans.COLOR_VIOLET:
holder.buubleText.setBackgroundResource(R.drawable.bubble_violet_edit);
holder.userName.setTextColor(context.getResources().getColor(R.color.bubble_violet));
holder.userName.setText(mUsers.getUser4());
break;
default:
break;
}
}
view.setTag(holder);
}
}
@Override
public Object getItem(int position) {
Bubble bubble = null;
if (cursor != null) {
int index = 0;
if (cursor.moveToFirst()) {
do {
if (index == position) {
bubble = mArdoiseDatabaseAdapter.mapBubble(context, cursor);
}
index++;
} while (cursor.moveToNext());
return bubble;
}
}
return bubble;
}
/**
* Get users.
*
* @return the mUsers
*/
public Users getmUsers() {
return mUsers;
}
/**
* Set users.
*
* @param mUsers
* the mUsers to set
*/
public void setmUsers(Users mUsers) {
this.mUsers = mUsers;
}
@Override
public int getCount() {
return cursor.getCount();
}
}
我希望有所帮助。
答案 1 :(得分:0)
无需选择....只需通过编程将“setBackgroundColor(Color.Blue)”写入你的布局.......
if(columnIndex == 6)
{
layout.setBackgroundColor(Color.Blue)
}
这是我的选择器xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/vhclistrowbackground" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@color/text_tab_unselected" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@color/text_tab_selected" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@color/text_tab_unselected" />
<!-- Pressed -->
<item android:state_pressed="true" android:drawable="@color/text_tab_unselected" />
</selector>
答案 2 :(得分:0)
我不太确定,但你可以试试:
if (columnIndex == 6) {
boolean dudoso = cursor.getInt(columnIndex) == 1;
TextView textView = (TextView) view;
textView.setText(dudoso ? "dudoso" : null);
if(dudoso)
{
v.setSelected(true);
}
return true;
}