Friday, August 17, 2012

Re: [android-developers] Re: CursorAdapter breaks CHOICE_MODE_MULTIPLE option

Yeah, that true remained there from debugging. It skips the loop, but
it' not a problem, and the action mode starts fine. It also closes and
reopens, no problem there.
My real problem is that every time I select an item, the selection
from the previous one is cleard. Also, it does work with arrayadapter,
without the setItemChecked call.
I followed this tutorial:
http://www.miximum.fr/tutos/849-porting-the-contextual-anction-mode-for-pre-honeycomb-android-apps

On Fri, Aug 17, 2012 at 6:09 PM, Kostya Vasilyev <kmansoft@gmail.com> wrote:
> Note sure what's going on, but this loop:
>
> boolean hasCheckedElement = true;
> for (int i = 0; i < checked.size() && !hasCheckedElement; i++) {
> hasCheckedElement = checked.valueAt(i);
> }
>
> will not execute ever, because "&&!hasCheckedElement" is always false, since
> "hasCheckedElement = true".
>
> The loop is redundant anyway, since the SparseBooleanArray is either null or
> empty, or has elements, which can be checked without a loop.
>
> I also don't see where you set "mActionMode" to null in the "finish" case.
>
> Finally, multiple-choice element selection is performed by
> ListView.setItemChecked(position, true/false), and I don't see your code
> calling that. If you did, the action mode would be started and terminated
> automatically, IIRC.
>
> Take all this with a grain of salt since I've not used ActionBarSherlock,
> but these are the things I would check.
>
> -- K
>
> 2012/8/17 Máté Gulyás <mgulyas86@gmail.com>
>>
>> That breaks it too.
>>
>> On Fri, Aug 17, 2012 at 5:25 PM, bob <bob@coolfone.comze.com> wrote:
>> > 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 android-developers@googlegroups.com
>> > To unsubscribe from this group, send email to
>> > android-developers+unsubscribe@googlegroups.com
>> > For more options, visit this group at
>> > http://groups.google.com/group/android-developers?hl=en
>>
>> --
>> You received this message because you are subscribed to the Google
>> Groups "Android Developers" group.
>> To post to this group, send email to android-developers@googlegroups.com
>> To unsubscribe from this group, send email to
>> android-developers+unsubscribe@googlegroups.com
>> For more options, visit this group at
>> http://groups.google.com/group/android-developers?hl=en
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscribe@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en

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

No comments:

Post a Comment