Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.supportv13.app;
     18 
     19 import android.app.ListFragment;
     20 import android.app.LoaderManager;
     21 import android.content.CursorLoader;
     22 import android.content.Loader;
     23 import android.database.Cursor;
     24 import android.net.Uri;
     25 import android.os.Bundle;
     26 import android.provider.ContactsContract.Contacts;
     27 import android.text.TextUtils;
     28 import android.util.Log;
     29 import android.view.Menu;
     30 import android.view.MenuInflater;
     31 import android.view.MenuItem;
     32 import android.view.View;
     33 import android.widget.ListView;
     34 import android.widget.SearchView;
     35 import android.widget.SimpleCursorAdapter;
     36 import android.widget.SearchView.OnQueryTextListener;
     37 
     38 
     39 public class CursorFragment extends ListFragment
     40         implements OnQueryTextListener, LoaderManager.LoaderCallbacks<Cursor> {
     41 
     42     // This is the Adapter being used to display the list's data.
     43     SimpleCursorAdapter mAdapter;
     44 
     45     // If non-null, this is the current filter the user has provided.
     46     String mCurFilter;
     47 
     48     @Override public void onActivityCreated(Bundle savedInstanceState) {
     49         super.onActivityCreated(savedInstanceState);
     50 
     51         // Give some text to display if there is no data.  In a real
     52         // application this would come from a resource.
     53         setEmptyText("No phone numbers");
     54 
     55         // We have a menu item to show in action bar.
     56         setHasOptionsMenu(true);
     57 
     58         // Create an empty adapter we will use to display the loaded data.
     59         mAdapter = new SimpleCursorAdapter(getActivity(),
     60                 android.R.layout.simple_list_item_2, null,
     61                 new String[] { Contacts.DISPLAY_NAME, Contacts.CONTACT_STATUS },
     62                 new int[] { android.R.id.text1, android.R.id.text2 }, 0);
     63         setListAdapter(mAdapter);
     64 
     65         // Start out with a progress indicator.
     66         setListShown(false);
     67 
     68         // Prepare the loader.  Either re-connect with an existing one,
     69         // or start a new one.
     70         getLoaderManager().initLoader(0, null, this);
     71     }
     72 
     73     @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
     74         // Place an action bar item for searching.
     75         MenuItem item = menu.add("Search");
     76         item.setIcon(android.R.drawable.ic_menu_search);
     77         item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM
     78                 | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
     79         SearchView sv = new SearchView(getActivity());
     80         sv.setOnQueryTextListener(this);
     81         item.setActionView(sv);
     82     }
     83 
     84     public boolean onQueryTextChange(String newText) {
     85         // Called when the action bar search text has changed.  Update
     86         // the search filter, and restart the loader to do a new query
     87         // with this filter.
     88         mCurFilter = !TextUtils.isEmpty(newText) ? newText : null;
     89         getLoaderManager().restartLoader(0, null, this);
     90         return true;
     91     }
     92 
     93     @Override public boolean onQueryTextSubmit(String query) {
     94         // Don't care about this.
     95         return true;
     96     }
     97 
     98     @Override public void onListItemClick(ListView l, View v, int position, long id) {
     99         // Insert desired behavior here.
    100         Log.i("FragmentComplexList", "Item clicked: " + id);
    101     }
    102 
    103     // These are the Contacts rows that we will retrieve.
    104     static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
    105         Contacts._ID,
    106         Contacts.DISPLAY_NAME,
    107         Contacts.CONTACT_STATUS,
    108         Contacts.CONTACT_PRESENCE,
    109         Contacts.PHOTO_ID,
    110         Contacts.LOOKUP_KEY,
    111     };
    112 
    113     public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    114         // This is called when a new Loader needs to be created.  This
    115         // sample only has one Loader, so we don't care about the ID.
    116         // First, pick the base URI to use depending on whether we are
    117         // currently filtering.
    118         Uri baseUri;
    119         if (mCurFilter != null) {
    120             baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI,
    121                     Uri.encode(mCurFilter));
    122         } else {
    123             baseUri = Contacts.CONTENT_URI;
    124         }
    125 
    126         // Now create and return a CursorLoader that will take care of
    127         // creating a Cursor for the data being displayed.
    128         String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
    129                 + Contacts.HAS_PHONE_NUMBER + "=1) AND ("
    130                 + Contacts.DISPLAY_NAME + " != '' ))";
    131         return new CursorLoader(getActivity(), baseUri,
    132                 CONTACTS_SUMMARY_PROJECTION, select, null,
    133                 Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
    134     }
    135 
    136     public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    137         // Swap the new cursor in.  (The framework will take care of closing the
    138         // old cursor once we return.)
    139         mAdapter.swapCursor(data);
    140 
    141         // The list should now be shown.
    142         if (isResumed()) {
    143             setListShown(true);
    144         } else {
    145             setListShownNoAnimation(true);
    146         }
    147     }
    148 
    149     public void onLoaderReset(Loader<Cursor> loader) {
    150         // This is called when the last Cursor provided to onLoadFinished()
    151         // above is about to be closed.  We need to make sure we are no
    152         // longer using it.
    153         mAdapter.swapCursor(null);
    154     }
    155 }
    156