Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, after quite a few dead ends I finally came up with a solution. Don't know if it is a good solution or if it is the "correct" way to do it but it works...</p> <p>I simply added a public ListItemCheckedListener variable to the CursorAdapter class, assigned my ListFragment ListItemCheckedListener instance to it straight after setting up the adapter and then could call the local instance of the listener within the CursorAdapter and the event would propagate out to the ListFragment.</p> <p>ListFragment:</p> <pre><code>public class ContactListFragment extends SherlockListFragment implements LoaderManager.LoaderCallbacks&lt;Cursor&gt;// , OnQueryTextListener { private ContactCursorAdapter _adapter; // &lt;----- HERE &lt;snip&gt; @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); setListItemListener(new ContactListItemCheckedListener() { @Override public void OnListItemChecked(int count) { if (count &gt; 0) { if (_mode == null) { _loaderManager = getLoaderManager(); ContactsActivity activity = (ContactsActivity)getActivity(); _mode = activity.startActionMode(_actionModeCallback); } if (_contactInvite != null) _contactInvite.setRecipCount(count); } else { if (_mode != null) { _mode.finish(); _mode = null; } } } }); _fragment = this; setListProcessedListener(new ContactListProcessedListener() { public void OnListProcessed(int contactMethod) { for (int i = 0; i &lt; _adapter.itemChecked.size(); i++) _adapter.itemChecked.set(i, false); _fragment.setListAdapter(_adapter); _loaderManager.initLoader(0, null, _fragment); } }); setEmptyText(this.getString(R.string.contacts_none)); setHasOptionsMenu(true); _adapter = new ContactCursorAdapter(getActivity(), R.layout.contact_list_item, null, new String[] { Contacts._ID, Contacts.DISPLAY_NAME, Contacts.CONTACT_STATUS }, CONTACT_SUMMARY_FIELDS, 0); _adapter.checkListener = checkListener; boolean[] iChecked = null; if (savedInstanceState != null) { if (savedInstanceState.containsKey("itemChecked")) { iChecked = savedInstanceState.getBooleanArray("itemChecked"); for (int i = 0; i &lt; iChecked.length; i++) _adapter.itemChecked.add(i, iChecked[i]); _adapter.countChecked = savedInstanceState.getInt("countChecked"); checkListener.OnListItemChecked(_adapter.countChecked); } } try { setListAdapter(_adapter); setListShown(false); // Prepare the loader. Either re-connect with an existing one, // or start a new one. getLoaderManager().initLoader(0, null, this); ((ContactsActivity)getActivity()).setSearchTextListener(new ContactsActivity.SearchTextListener() { @Override public void OnSearchTextChange(String text) { onQueryTextChange(text); } }); } catch (Exception e) { Singletons.Logger().error(TAG + ".onActivityCreated() failed", e.getMessage()); } } </code></pre> <p>CursorAdapter:</p> <pre><code> public class ContactCursorAdapter extends SimpleCursorAdapter { &lt;snip&gt; public ListItemCheckedListener checkListener; // &lt;----- HERE &lt;snip&gt; @Override public void bindView(View view, Context context, Cursor cursor) { &lt;snip&gt; cBox.setOnClickListener(new OnClickListener() { public void onClick(View view) { CheckBox cb = (CheckBox)view.findViewById(R.id.checkBox); if (cb.isChecked()) { itemChecked.set(pos, true); countChecked++; checkListener.OnListItemChecked(countChecked); // &lt;----- HERE } else if (!cb.isChecked()) { itemChecked.set(pos, false); countChecked--; checkListener.OnListItemChecked(countChecked); // &lt;----- HERE } } }); cBox.setChecked(itemChecked.get(pos)); } </code></pre> <p>Quite simple and fairly elegant, if I do say so myself. Well... at least it is in comparison to some of my earlier failed attempts(!). I still get the feeling that it isn't the best way to do it, somehow just feels wrong, but hey... it works.</p> <p>If anyone has any better ways of doing this, I'd love to hear them. In the meanwhile, I hope this helps someone else.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload