blob: 07b9309a1676c0401dbebe6c239514e2de59bca7 [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;
25import android.support.v4.widget.SimpleCursorAdapter;
26
27import android.database.Cursor;
28import android.net.Uri;
29import android.os.Bundle;
30import android.provider.ContactsContract.Contacts;
31import android.text.TextUtils;
32import android.util.Log;
33import android.view.Menu;
34import android.view.MenuInflater;
Dianne Hackborn9fd3b6e2011-02-01 10:38:02 -080035import android.view.View;
36import android.widget.ListView;
37
38/**
Dianne Hackbornd9012d12011-04-04 18:52:04 -070039 * Demonstration of the use of a CursorLoader to load and display contacts
40 * data in a fragment.
Dianne Hackborn9fd3b6e2011-02-01 10:38:02 -080041 */
Dianne Hackbornd9012d12011-04-04 18:52:04 -070042public class LoaderCursorSupport extends FragmentActivity {
Dianne Hackborn9fd3b6e2011-02-01 10:38:02 -080043
44 @Override
45 protected void onCreate(Bundle savedInstanceState) {
46 super.onCreate(savedInstanceState);
47
48 FragmentManager fm = getSupportFragmentManager();
49
50 // Create the list fragment and add it as our sole content.
51 if (fm.findFragmentById(android.R.id.content) == null) {
52 CursorLoaderListFragment list = new CursorLoaderListFragment();
53 fm.beginTransaction().add(android.R.id.content, list).commit();
54 }
55 }
56
57//BEGIN_INCLUDE(fragment_cursor)
58 public static class CursorLoaderListFragment extends ListFragment
59 implements LoaderManager.LoaderCallbacks<Cursor> {
60
61 // This is the Adapter being used to display the list's data.
62 SimpleCursorAdapter mAdapter;
63
64 // If non-null, this is the current filter the user has provided.
65 String mCurFilter;
66
67 @Override public void onActivityCreated(Bundle savedInstanceState) {
68 super.onActivityCreated(savedInstanceState);
69
70 // Give some text to display if there is no data. In a real
71 // application this would come from a resource.
72 setEmptyText("No phone numbers");
73
74 // We have a menu item to show in action bar.
75 setHasOptionsMenu(true);
76
77 // Create an empty adapter we will use to display the loaded data.
78 mAdapter = new SimpleCursorAdapter(getActivity(),
79 android.R.layout.simple_list_item_2, null,
80 new String[] { Contacts.DISPLAY_NAME, Contacts.CONTACT_STATUS },
81 new int[] { android.R.id.text1, android.R.id.text2 }, 0);
82 setListAdapter(mAdapter);
83
84 // Prepare the loader. Either re-connect with an existing one,
85 // or start a new one.
86 getLoaderManager().initLoader(0, null, this);
87 }
88
89 @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
90 // Place an action bar item for searching.
91 //MenuItem item = menu.add("Search");
92 //item.setIcon(android.R.drawable.ic_menu_search);
93 //item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
94 //SearchView sv = new SearchView(getActivity());
95 //sv.setOnQueryTextListener(this);
96 //item.setActionView(sv);
97 }
98
99 public boolean onQueryTextChange(String newText) {
100 // Called when the action bar search text has changed. Update
101 // the search filter, and restart the loader to do a new query
102 // with this filter.
103 mCurFilter = !TextUtils.isEmpty(newText) ? newText : null;
104 getLoaderManager().restartLoader(0, null, this);
105 return true;
106 }
107
108 @Override public void onListItemClick(ListView l, View v, int position, long id) {
109 // Insert desired behavior here.
110 Log.i("FragmentComplexList", "Item clicked: " + id);
111 }
112
113 // These are the Contacts rows that we will retrieve.
114 static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
115 Contacts._ID,
116 Contacts.DISPLAY_NAME,
117 Contacts.CONTACT_STATUS,
118 Contacts.CONTACT_PRESENCE,
119 Contacts.PHOTO_ID,
120 Contacts.LOOKUP_KEY,
121 };
122
123 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
124 // This is called when a new Loader needs to be created. This
125 // sample only has one Loader, so we don't care about the ID.
126 // First, pick the base URI to use depending on whether we are
127 // currently filtering.
128 Uri baseUri;
129 if (mCurFilter != null) {
130 baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI,
131 Uri.encode(mCurFilter));
132 } else {
133 baseUri = Contacts.CONTENT_URI;
134 }
135
136 // Now create and return a CursorLoader that will take care of
137 // creating a Cursor for the data being displayed.
138 String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
139 + Contacts.HAS_PHONE_NUMBER + "=1) AND ("
140 + Contacts.DISPLAY_NAME + " != '' ))";
141 return new CursorLoader(getActivity(), baseUri,
142 CONTACTS_SUMMARY_PROJECTION, select, null,
143 Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
144 }
145
146 public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
147 // Swap the new cursor in. (The framework will take care of closing the
148 // old cursor once we return.)
149 mAdapter.swapCursor(data);
150 }
151
152 public void onLoaderReset(Loader<Cursor> loader) {
153 // This is called when the last Cursor provided to onLoadFinished()
154 // above is about to be closed. We need to make sure we are no
155 // longer using it.
156 mAdapter.swapCursor(null);
157 }
158 }
159//END_INCLUDE(fragment_cursor)
160}