0

カーソルの現在のメンバーのプロパティに基づいて、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;
    }
});

しかし、それは機能していません。私がやりたいことをする正しい方法は何ですか?

4

3 に答える 3

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();
}
}

お役に立てば幸いです。

于 2012-12-20T08:45:07.910 に答える
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> 
于 2012-12-20T08:34:41.677 に答える
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;
    }
于 2012-12-20T13:56:47.693 に答える