@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);
   }
 }
  @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 onLoaderReset(Loader<Cursor> loader) {
   if (loader.getId() == ContactsQuery.QUERY_ID) {
     // When the loader is being reset, clear the cursor from the adapter. This allows the cursor
     // resources to be freed.
     mAdapter.swapCursor(null);
   }
 }