LoaderCursorSupport.java revision d353bdc6af02300eff568e522f1dbfa79a119768
1/* 2 * Copyright (C) 2010 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 17package com.example.android.supportv4.app; 18 19import android.database.Cursor; 20import android.net.Uri; 21import android.os.Bundle; 22import android.provider.Contacts.People; 23import android.support.v4.app.FragmentActivity; 24import android.support.v4.app.FragmentManager; 25import android.support.v4.app.ListFragment; 26import android.support.v4.app.LoaderManager; 27import android.support.v4.content.CursorLoader; 28import android.support.v4.content.Loader; 29import android.support.v4.view.MenuItemCompat; 30import android.support.v4.widget.SimpleCursorAdapter; 31import android.text.TextUtils; 32import android.util.Log; 33import android.view.Menu; 34import android.view.MenuInflater; 35import android.view.MenuItem; 36import android.view.View; 37import android.widget.ListView; 38import android.widget.SearchView; 39 40/** 41 * Demonstration of the use of a CursorLoader to load and display contacts 42 * data in a fragment. 43 */ 44@SuppressWarnings("all") 45public class LoaderCursorSupport extends FragmentActivity { 46 47 @Override 48 protected void onCreate(Bundle savedInstanceState) { 49 super.onCreate(savedInstanceState); 50 51 FragmentManager fm = getSupportFragmentManager(); 52 53 // Create the list fragment and add it as our sole content. 54 if (fm.findFragmentById(android.R.id.content) == null) { 55 CursorLoaderListFragment list = new CursorLoaderListFragment(); 56 fm.beginTransaction().add(android.R.id.content, list).commit(); 57 } 58 } 59 60//BEGIN_INCLUDE(fragment_cursor) 61 public static class CursorLoaderListFragment extends ListFragment 62 implements LoaderManager.LoaderCallbacks<Cursor> { 63 64 // This is the Adapter being used to display the list's data. 65 SimpleCursorAdapter mAdapter; 66 67 // If non-null, this is the current filter the user has provided. 68 String mCurFilter; 69 70 @Override public void onActivityCreated(Bundle savedInstanceState) { 71 super.onActivityCreated(savedInstanceState); 72 73 // Give some text to display if there is no data. In a real 74 // application this would come from a resource. 75 setEmptyText("No phone numbers"); 76 77 // We have a menu item to show in action bar. 78 setHasOptionsMenu(true); 79 80 // Create an empty adapter we will use to display the loaded data. 81 mAdapter = new SimpleCursorAdapter(getActivity(), 82 android.R.layout.simple_list_item_1, null, 83 new String[] { People.DISPLAY_NAME }, 84 new int[] { android.R.id.text1}, 0); 85 setListAdapter(mAdapter); 86 87 // Start out with a progress indicator. 88 setListShown(false); 89 90 // Prepare the loader. Either re-connect with an existing one, 91 // or start a new one. 92 getLoaderManager().initLoader(0, null, this); 93 } 94 95 @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 96 // Place an action bar item for searching. 97 MenuItem item = menu.add("Search"); 98 item.setIcon(android.R.drawable.ic_menu_search); 99 MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_ALWAYS 100 | MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); 101 final SearchView searchView = new SearchView(getActivity()); 102 searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { 103 @Override 104 public boolean onQueryTextChange(String newText) { 105 // Called when the action bar search text has changed. Update 106 // the search filter, and restart the loader to do a new query 107 // with this filter. 108 String newFilter = !TextUtils.isEmpty(newText) ? newText : null; 109 // Don't do anything if the filter hasn't actually changed. 110 // Prevents restarting the loader when restoring state. 111 if (mCurFilter == null && newFilter == null) { 112 return true; 113 } 114 if (mCurFilter != null && mCurFilter.equals(newFilter)) { 115 return true; 116 } 117 mCurFilter = newFilter; 118 getLoaderManager().restartLoader(0, null, CursorLoaderListFragment.this); 119 return true; 120 } 121 122 @Override 123 public boolean onQueryTextSubmit(String s) { 124 return false; 125 } 126 }); 127 searchView.setOnCloseListener(new SearchView.OnCloseListener() { 128 @Override 129 public boolean onClose() { 130 if (!TextUtils.isEmpty(searchView.getQuery())) { 131 searchView.setQuery(null, true); 132 } 133 return true; 134 } 135 }); 136 MenuItemCompat.setActionView(item, searchView); 137 } 138 139 @Override public void onListItemClick(ListView l, View v, int position, long id) { 140 // Insert desired behavior here. 141 Log.i("FragmentComplexList", "Item clicked: " + id); 142 } 143 144 // These are the Contacts rows that we will retrieve. 145 static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { 146 People._ID, 147 People.DISPLAY_NAME, 148 }; 149 150 public Loader<Cursor> onCreateLoader(int id, Bundle args) { 151 // This is called when a new Loader needs to be created. This 152 // sample only has one Loader, so we don't care about the ID. 153 // First, pick the base URI to use depending on whether we are 154 // currently filtering. 155 Uri baseUri; 156 if (mCurFilter != null) { 157 baseUri = Uri.withAppendedPath(People.CONTENT_FILTER_URI, Uri.encode(mCurFilter)); 158 } else { 159 baseUri = People.CONTENT_URI; 160 } 161 162 // Now create and return a CursorLoader that will take care of 163 // creating a Cursor for the data being displayed. 164 String select = "((" + People.DISPLAY_NAME + " NOTNULL) AND (" 165 + People.DISPLAY_NAME + " != '' ))"; 166 return new CursorLoader(getActivity(), baseUri, 167 CONTACTS_SUMMARY_PROJECTION, select, null, 168 People.DISPLAY_NAME + " COLLATE LOCALIZED ASC"); 169 } 170 171 public void onLoadFinished(Loader<Cursor> loader, Cursor data) { 172 // Swap the new cursor in. (The framework will take care of closing the 173 // old cursor once we return.) 174 mAdapter.swapCursor(data); 175 176 // The list should now be shown. 177 if (isResumed()) { 178 setListShown(true); 179 } else { 180 setListShownNoAnimation(true); 181 } 182 } 183 184 public void onLoaderReset(Loader<Cursor> loader) { 185 // This is called when the last Cursor provided to onLoadFinished() 186 // above is about to be closed. We need to make sure we are no 187 // longer using it. 188 mAdapter.swapCursor(null); 189 } 190 } 191//END_INCLUDE(fragment_cursor) 192} 193