blob: dd4237b7d34ad686e7985571500723bc6f942fc4 [file] [log] [blame]
Dianne Hackborn9fd3b6e2011-02-01 10:38:02 -08001/*
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
Dianne Hackborn10c33522011-05-27 18:29:00 -070017package com.example.android.supportv4.app;
Dianne Hackborn9fd3b6e2011-02-01 10:38:02 -080018
19import android.support.v4.app.FragmentActivity;
20import android.support.v4.app.FragmentManager;
21import android.support.v4.app.ListFragment;
22import android.support.v4.app.LoaderManager;
23import android.support.v4.content.CursorLoader;
24import android.support.v4.content.Loader;
Svetoslav Ganova09e21a2011-11-14 16:37:43 -080025import android.support.v4.view.MenuItemCompat;
26import android.support.v4.widget.SearchViewCompat;
27import android.support.v4.widget.SearchViewCompat.OnQueryTextListenerCompat;
Dianne Hackborn9fd3b6e2011-02-01 10:38:02 -080028import android.support.v4.widget.SimpleCursorAdapter;
29
30import android.database.Cursor;
31import android.net.Uri;
32import android.os.Bundle;
Svetoslav Ganova09e21a2011-11-14 16:37:43 -080033import android.provider.BaseColumns;
34import android.provider.Contacts.People;
Dianne Hackborn9fd3b6e2011-02-01 10:38:02 -080035import android.text.TextUtils;
36import android.util.Log;
37import android.view.Menu;
38import android.view.MenuInflater;
Svetoslav Ganova09e21a2011-11-14 16:37:43 -080039import android.view.MenuItem;
Dianne Hackborn9fd3b6e2011-02-01 10:38:02 -080040import android.view.View;
41import android.widget.ListView;
42
43/**
Dianne Hackbornd9012d12011-04-04 18:52:04 -070044 * Demonstration of the use of a CursorLoader to load and display contacts
45 * data in a fragment.
Dianne Hackborn9fd3b6e2011-02-01 10:38:02 -080046 */
Svetoslav Ganova09e21a2011-11-14 16:37:43 -080047@SuppressWarnings("all")
Dianne Hackbornd9012d12011-04-04 18:52:04 -070048public class LoaderCursorSupport extends FragmentActivity {
Dianne Hackborn9fd3b6e2011-02-01 10:38:02 -080049
50 @Override
51 protected void onCreate(Bundle savedInstanceState) {
52 super.onCreate(savedInstanceState);
53
54 FragmentManager fm = getSupportFragmentManager();
55
56 // Create the list fragment and add it as our sole content.
57 if (fm.findFragmentById(android.R.id.content) == null) {
58 CursorLoaderListFragment list = new CursorLoaderListFragment();
59 fm.beginTransaction().add(android.R.id.content, list).commit();
60 }
61 }
62
63//BEGIN_INCLUDE(fragment_cursor)
64 public static class CursorLoaderListFragment extends ListFragment
65 implements LoaderManager.LoaderCallbacks<Cursor> {
66
67 // This is the Adapter being used to display the list's data.
68 SimpleCursorAdapter mAdapter;
69
70 // If non-null, this is the current filter the user has provided.
71 String mCurFilter;
72
73 @Override public void onActivityCreated(Bundle savedInstanceState) {
74 super.onActivityCreated(savedInstanceState);
75
76 // Give some text to display if there is no data. In a real
77 // application this would come from a resource.
78 setEmptyText("No phone numbers");
79
80 // We have a menu item to show in action bar.
81 setHasOptionsMenu(true);
82
83 // Create an empty adapter we will use to display the loaded data.
84 mAdapter = new SimpleCursorAdapter(getActivity(),
Svetoslav Ganova09e21a2011-11-14 16:37:43 -080085 android.R.layout.simple_list_item_1, null,
86 new String[] { People.DISPLAY_NAME },
87 new int[] { android.R.id.text1}, 0);
Dianne Hackborn9fd3b6e2011-02-01 10:38:02 -080088 setListAdapter(mAdapter);
89
Dianne Hackbornbe6b6b42011-06-13 13:48:13 -070090 // Start out with a progress indicator.
91 setListShown(false);
92
Dianne Hackborn9fd3b6e2011-02-01 10:38:02 -080093 // Prepare the loader. Either re-connect with an existing one,
94 // or start a new one.
95 getLoaderManager().initLoader(0, null, this);
96 }
97
98 @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
99 // Place an action bar item for searching.
Svetoslav Ganova09e21a2011-11-14 16:37:43 -0800100 MenuItem item = menu.add("Search");
101 item.setIcon(android.R.drawable.ic_menu_search);
102 MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
103 View searchView = SearchViewCompat.newSearchView(getActivity());
104 if (searchView != null) {
105 SearchViewCompat.setOnQueryTextListener(searchView,
106 new OnQueryTextListenerCompat() {
107 @Override
108 public boolean onQueryTextChange(String newText) {
109 // Called when the action bar search text has changed. Update
110 // the search filter, and restart the loader to do a new query
111 // with this filter.
112 mCurFilter = !TextUtils.isEmpty(newText) ? newText : null;
113 getLoaderManager().restartLoader(0, null, CursorLoaderListFragment.this);
114 return true;
115 }
116 });
117 MenuItemCompat.setActionView(item, searchView);
118 }
Dianne Hackborn9fd3b6e2011-02-01 10:38:02 -0800119 }
120
121 @Override public void onListItemClick(ListView l, View v, int position, long id) {
122 // Insert desired behavior here.
123 Log.i("FragmentComplexList", "Item clicked: " + id);
124 }
125
126 // These are the Contacts rows that we will retrieve.
127 static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
Svetoslav Ganova09e21a2011-11-14 16:37:43 -0800128 People._ID,
129 People.DISPLAY_NAME,
Dianne Hackborn9fd3b6e2011-02-01 10:38:02 -0800130 };
131
132 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
133 // This is called when a new Loader needs to be created. This
134 // sample only has one Loader, so we don't care about the ID.
135 // First, pick the base URI to use depending on whether we are
136 // currently filtering.
137 Uri baseUri;
138 if (mCurFilter != null) {
Svetoslav Ganova09e21a2011-11-14 16:37:43 -0800139 baseUri = Uri.withAppendedPath(People.CONTENT_FILTER_URI, Uri.encode(mCurFilter));
Dianne Hackborn9fd3b6e2011-02-01 10:38:02 -0800140 } else {
Svetoslav Ganova09e21a2011-11-14 16:37:43 -0800141 baseUri = People.CONTENT_URI;
Dianne Hackborn9fd3b6e2011-02-01 10:38:02 -0800142 }
143
144 // Now create and return a CursorLoader that will take care of
145 // creating a Cursor for the data being displayed.
Svetoslav Ganova09e21a2011-11-14 16:37:43 -0800146 String select = "((" + People.DISPLAY_NAME + " NOTNULL) AND ("
147 + People.DISPLAY_NAME + " != '' ))";
Dianne Hackborn9fd3b6e2011-02-01 10:38:02 -0800148 return new CursorLoader(getActivity(), baseUri,
149 CONTACTS_SUMMARY_PROJECTION, select, null,
Svetoslav Ganova09e21a2011-11-14 16:37:43 -0800150 People.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
Dianne Hackborn9fd3b6e2011-02-01 10:38:02 -0800151 }
152
153 public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
154 // Swap the new cursor in. (The framework will take care of closing the
155 // old cursor once we return.)
156 mAdapter.swapCursor(data);
Dianne Hackbornbe6b6b42011-06-13 13:48:13 -0700157
158 // The list should now be shown.
159 if (isResumed()) {
160 setListShown(true);
161 } else {
162 setListShownNoAnimation(true);
163 }
Dianne Hackborn9fd3b6e2011-02-01 10:38:02 -0800164 }
165
166 public void onLoaderReset(Loader<Cursor> loader) {
167 // This is called when the last Cursor provided to onLoadFinished()
168 // above is about to be closed. We need to make sure we are no
169 // longer using it.
170 mAdapter.swapCursor(null);
171 }
172 }
173//END_INCLUDE(fragment_cursor)
174}