基于财产更改背景颜色 [英] Change background color based on property
本文介绍了基于财产更改背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要cheange基础上光标的当前成员的一些属性列表项的背景的颜色。我使用SimpleCursorAdapter绑定数据库到ListView的值。我所做的就是创建一个颜色选择是这样的:
I want to cheange the color of the background of a ListItem based on some property of the current member of the cursor. I'm using SimpleCursorAdapter to bind the values of the database to the ListView. What I did is to create a color selector like this:
<?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" >
...
...
而我在那里我做了以下视图粘结剂:
And the I have a view binder where I do the following:
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;
}
});
但它不工作。什么是做我想做正确的方法是什么?
But it's not working. What is the correct way to do what I want?
推荐答案
也许一个更好的办法是写自己的类从SimpleCursorAdapter继承。这样的事情:
Perhaps a better way would be to write your own class that inherits from the SimpleCursorAdapter. Something like that:
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();
}
}
我希望帮助。
这篇关于基于财产更改背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文