/** * Called when ListView selection is cleared, for example when search mode is finished and the * currently selected contact should no longer be selected. */ private void onSelectionCleared() { // Uses callback to notify activity this contains this fragment mOnContactSelectedListener.onSelectionCleared(); // Clears currently checked item getListView().clearChoices(); }
@Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { // This swaps the new cursor into the adapter. if (loader.getId() == ContactsQuery.QUERY_ID) { mAdapter.swapCursor(data); if (!TextUtils.isEmpty(mSearchTerm) && mSearchQueryChanged) { // Selects the first item in results, unless this fragment has // been restored from a saved state (like orientation change) // in which case it selects the previously selected search item. if (data != null && data.moveToPosition(mPreviouslySelectedSearchItem)) { // Creates the content Uri for the previously selected // contact by appending the // contact's ID to the Contacts table content Uri final Uri uri = Uri.withAppendedPath( Contacts.CONTENT_URI, String.valueOf(data.getLong(ContactsQuery.ID))); mOnContactSelectedListener.onContactSelected(uri); getListView().setItemChecked(mPreviouslySelectedSearchItem, true); } else { // No results, clear selection. onSelectionCleared(); } // Only restore from saved state one time. Next time fall back // to selecting first item. If the fragment state is saved again // then the currently selected item will once again be saved. mPreviouslySelectedSearchItem = 0; mSearchQueryChanged = false; } } }
@Override public void onItemClick(AdapterView<?> parent, View v, int position, long id) { CheckBox checkbox = (CheckBox) v.findViewWithTag(position); if (checkbox.isChecked()) checkbox.setChecked(false); else checkbox.setChecked(true); // Gets the Cursor object currently bound to the ListView final Cursor cursor = mAdapter.getCursor(); // Moves to the Cursor row corresponding to the ListView item that was // clicked cursor.moveToPosition(position); // Creates a contact lookup Uri from contact ID and lookup_key final Uri uri = Contacts.getLookupUri( cursor.getLong(ContactsQuery.ID), cursor.getString(ContactsQuery.LOOKUP_KEY)); // Notifies the parent activity that the user selected a contact. In a // two-pane layout, the // parent activity loads a ContactDetailFragment that displays the // details for the selected // contact. In a single-pane layout, the parent activity starts a new // activity that // displays contact details in its own Fragment. mOnContactSelectedListener.onContactSelected(uri); // If two-pane layout sets the selected item to checked so it remains // highlighted. In a // single-pane layout a new activity is started so this is not needed. }
@Override public void onItemClick(AdapterView<?> parent, View v, int position, long id) { // Gets the Cursor object currently bound to the ListView final Cursor cursor = mAdapter.getCursor(); // Moves to the Cursor row corresponding to the ListView item that was clicked cursor.moveToPosition(position); // Creates a contact lookup Uri from contact ID and lookup_key final Uri uri = Contacts.getLookupUri( cursor.getLong(ContactsQuery.ID), cursor.getString(ContactsQuery.LOOKUP_KEY)); // NOTIFIES THE PARENT ACTIVITY that the user selected a contact. Then the parent activity // starts a new activity that // displays contact details in its own Fragment. mOnContactSelectedListener.onContactSelected(uri); }