What about a SimpleCursorAdapter?

On Friday, August 17, 2012 9:14:09 AM UTC-5, Máté Gulyás wrote:
>
>
>
> 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 simpleArrayAdapter. 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.
>
>
> Máté
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to