Search code examples
androidlistviewandroid-arrayadapterandroid-cursoradapter

CursorAdapter breaks CHOICE_MODE_MULTIPLE option


I have a ListFragment, where I add a CursorAdapter to my ListView, and I want to be able to click several rows, to use contextual action bar. I use SherlockActionbar, and it works fine, when I use a simple ArrayAdapter. But when I switch to CursorAdapter, it breaks. I cannot select multiple rows, only one. Any idea why it might happen?

In onActivityCreated I set up the list:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mActionMode = null;
    mListView = getListView();
    FinTracDatabase database = mDatabaseProvider.get();
    Cursor cursor = database.getTransactionCursor(false);
    mCursorAdapter = new TransactionListAdapter(getSherlockActivity(), cursor);
    mListView.setAdapter(mCursorAdapter);
    mListView.setItemsCanFocus(false);
    mListView.setOnItemClickListener(this);
    mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}

This is my Adapter:

private class TransactionListAdapter extends CursorAdapter {

    public TransactionListAdapter(Context context, Cursor cursor) {
        super(context, cursor, 0);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        bindToExistingView(view, cursor);
    }

    private void bindToExistingView(View view, Cursor cursor) {
        CheckedTextView amountView = (CheckedTextView) view;
        amountView.setText(cursor.getString(cursor.getColumnIndex(Transactions.TITLE)));
    }

    @Override
    public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
        LayoutInflater layoutInflater = getSherlockActivity().getLayoutInflater();
        View view = layoutInflater.inflate(android.R.layout.simple_list_item_multiple_choice, arg2, false);
        bindToExistingView(view, arg1);
        return view;
    }

}

And finally the onClickListener:

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    SparseBooleanArray checked = mListView.getCheckedItemPositions();
    boolean hasCheckedElement = true;
    for (int i = 0; i < checked.size() && !hasCheckedElement; i++) {
        hasCheckedElement = checked.valueAt(i);
    }

    if (hasCheckedElement) {
        if (mActionMode == null) {
            mActionMode = getSherlockActivity().startActionMode(new SelectingActionMode());
        }
    } else {
        if (mActionMode != null) {
            mActionMode.finish();
        }
    }
}

If I switch my Adapter to a simple ArrayAdapter, it works fine.

new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_multiple_choice, new String[]{"A", "B", "C"})

I am hopeless, and I have no idea, why this is happening.


Solution

  • In order for the ListView.CHOICE_MODE_MULTIPLE mode to work properly, each of the item in the adapter must return a unique value from the getItemId() method.

    The cursor you are using for your adapter is produced on these lines:

      FinTracDatabase database = mDatabaseProvider.get();
      Cursor cursor = database.getTransactionCursor(false);
    

    Could you check whether it has unique values on its "_id" column for each row? I am suspecting that they all share the same values, resulting in the behavior that you see.