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.SearchView.OnQueryTextListener; 36 import android.widget.SimpleCursorAdapter; 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 onActivityCreated(Bundle savedInstanceState)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 onCreateOptionsMenu(Menu menu, MenuInflater inflater)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 @Override onQueryTextChange(String newText)85 public boolean onQueryTextChange(String newText) { 86 // Called when the action bar search text has changed. Update 87 // the search filter, and restart the loader to do a new query 88 // with this filter. 89 mCurFilter = !TextUtils.isEmpty(newText) ? newText : null; 90 getLoaderManager().restartLoader(0, null, this); 91 return true; 92 } 93 onQueryTextSubmit(String query)94 @Override public boolean onQueryTextSubmit(String query) { 95 // Don't care about this. 96 return true; 97 } 98 onListItemClick(ListView l, View v, int position, long id)99 @Override public void onListItemClick(ListView l, View v, int position, long id) { 100 // Insert desired behavior here. 101 Log.i("FragmentComplexList", "Item clicked: " + id); 102 } 103 104 // These are the Contacts rows that we will retrieve. 105 static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { 106 Contacts._ID, 107 Contacts.DISPLAY_NAME, 108 Contacts.CONTACT_STATUS, 109 Contacts.CONTACT_PRESENCE, 110 Contacts.PHOTO_ID, 111 Contacts.LOOKUP_KEY, 112 }; 113 114 @Override onCreateLoader(int id, Bundle args)115 public Loader<Cursor> onCreateLoader(int id, Bundle args) { 116 // This is called when a new Loader needs to be created. This 117 // sample only has one Loader, so we don't care about the ID. 118 // First, pick the base URI to use depending on whether we are 119 // currently filtering. 120 Uri baseUri; 121 if (mCurFilter != null) { 122 baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, 123 Uri.encode(mCurFilter)); 124 } else { 125 baseUri = Contacts.CONTENT_URI; 126 } 127 128 // Now create and return a CursorLoader that will take care of 129 // creating a Cursor for the data being displayed. 130 String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND (" 131 + Contacts.HAS_PHONE_NUMBER + "=1) AND (" 132 + Contacts.DISPLAY_NAME + " != '' ))"; 133 return new CursorLoader(getActivity(), baseUri, 134 CONTACTS_SUMMARY_PROJECTION, select, null, 135 Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"); 136 } 137 138 @Override onLoadFinished(Loader<Cursor> loader, Cursor data)139 public void onLoadFinished(Loader<Cursor> loader, Cursor data) { 140 // Swap the new cursor in. (The framework will take care of closing the 141 // old cursor once we return.) 142 mAdapter.swapCursor(data); 143 144 // The list should now be shown. 145 if (isResumed()) { 146 setListShown(true); 147 } else { 148 setListShownNoAnimation(true); 149 } 150 } 151 152 @Override onLoaderReset(Loader<Cursor> loader)153 public void onLoaderReset(Loader<Cursor> loader) { 154 // This is called when the last Cursor provided to onLoadFinished() 155 // above is about to be closed. We need to make sure we are no 156 // longer using it. 157 mAdapter.swapCursor(null); 158 } 159 } 160