Merge branch 'master' into honeycomb-release

* master:
  Update fragment cursor loader sample to new New and Shiny.
  Prepare for some changes to LoaderManager/Loader.
This commit is contained in:
The Android Automerger
2010-12-17 13:44:50 -08:00

View File

@@ -25,17 +25,20 @@ import android.content.Context;
import android.content.CursorLoader; import android.content.CursorLoader;
import android.content.Loader; import android.content.Loader;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.provider.ContactsContract.Contacts; import android.provider.ContactsContract.Contacts;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.widget.ListAdapter;
import android.widget.ListView; import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SimpleCursorAdapter; import android.widget.SimpleCursorAdapter;
import android.widget.SearchView.OnQueryChangeListener;
/** /**
* Demonstration of more complex use if a ListFragment, including showing * Demonstration of more complex use if a ListFragment, including showing
@@ -54,12 +57,17 @@ public class FragmentListCursorLoader extends Activity {
} }
} }
//BEGIN_INCLUDE(fragment_cursor)
public static class CursorLoaderListFragment extends ListFragment public static class CursorLoaderListFragment extends ListFragment
implements LoaderManager.LoaderCallbacks<Cursor> { implements OnQueryChangeListener, LoaderManager.LoaderCallbacks<Cursor> {
MenuItem mSearchMenu;
@Override // This is the Adapter being used to display the list's data.
public void onActivityCreated(Bundle savedInstanceState) { SimpleCursorAdapter mAdapter;
// If non-null, this is the current filter the user has provided.
String mCurFilter;
@Override public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
// Give some text to display if there is no data. In a real // Give some text to display if there is no data. In a real
@@ -74,30 +82,40 @@ public class FragmentListCursorLoader extends Activity {
getLoaderManager().initLoader(0, null, this); getLoaderManager().initLoader(0, null, this);
} }
@Override @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { // Place an action bar item for searching.
mSearchMenu = menu.add("Search"); MenuItem item = menu.add("Search");
mSearchMenu.setIcon(R.drawable.magnifying_glass); item.setIcon(android.R.drawable.ic_menu_search);
mSearchMenu.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
SearchView sv = new SearchView(getActivity());
sv.setOnQueryChangeListener(this);
item.setActionView(sv);
} }
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onQueryTextChanged(String newText) {
if (item == mSearchMenu) { // Called when the action bar search text has changed. Update
InputMethodManager imm = (InputMethodManager)getActivity() // the search filter, and restart the loader to do a new query
.getSystemService(Context.INPUT_METHOD_SERVICE); // with this filter.
imm.showSoftInput(getActivity().getCurrentFocus(), 0); Log.i("foo", "onQueryTextChanged: " + newText);
return true; mCurFilter = !TextUtils.isEmpty(newText) ? newText : null;
} else { getLoaderManager().restartLoader(0, null, this);
return super.onOptionsItemSelected(item); return true;
} }
@Override
public boolean onSubmitQuery(String query) {
// Don't care about this.
return true;
} }
@Override @Override
public void onListItemClick(ListView l, View v, int position, long id) { public void onListItemClick(ListView l, View v, int position, long id) {
// Insert desired behavior here.
Log.i("FragmentComplexList", "Item clicked: " + id); Log.i("FragmentComplexList", "Item clicked: " + id);
} }
// These are the Contacts rows that we will retrieve.
static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
Contacts._ID, Contacts._ID,
Contacts.DISPLAY_NAME, Contacts.DISPLAY_NAME,
@@ -108,20 +126,54 @@ public class FragmentListCursorLoader extends Activity {
}; };
public Loader<Cursor> onCreateLoader(int id, Bundle args) { public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// This is called when a new Loader needs to be created. This
// sample only has one Loader, so we don't care about the ID.
// First, pick the base URI to use depending on whether we are
// currently filtering.
Uri baseUri;
if (mCurFilter != null) {
baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI,
Uri.encode(mCurFilter));
} else {
baseUri = Contacts.CONTENT_URI;
}
// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND (" String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
+ Contacts.HAS_PHONE_NUMBER + "=1) AND (" + Contacts.HAS_PHONE_NUMBER + "=1) AND ("
+ Contacts.DISPLAY_NAME + " != '' ))"; + Contacts.DISPLAY_NAME + " != '' ))";
return new CursorLoader(getActivity(), Contacts.CONTENT_URI, return new CursorLoader(getActivity(), baseUri,
CONTACTS_SUMMARY_PROJECTION, select, null, CONTACTS_SUMMARY_PROJECTION, select, null,
Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"); Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
} }
public void onLoadFinished(Loader<Cursor> loader, Cursor data) { public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
ListAdapter adapter = new SimpleCursorAdapter(getActivity(), // This is called when the last created Loader has its data
android.R.layout.simple_list_item_2, data, // available to be displayed.
new String[] { Contacts.DISPLAY_NAME, Contacts.CONTACT_STATUS }, if (mAdapter == null) {
new int[] { android.R.id.text1, android.R.id.text2 }); // If this is the first time through, create a new Adapter
setListAdapter(adapter); // in which to display the data with the Cursor being given
// here as the initial data source.
mAdapter = new SimpleCursorAdapter(getActivity(),
android.R.layout.simple_list_item_2, data,
new String[] { Contacts.DISPLAY_NAME, Contacts.CONTACT_STATUS },
new int[] { android.R.id.text1, android.R.id.text2 }, 0);
setListAdapter(mAdapter);
} else {
// If we have already created the adapter, just swap in the
// new Cursor. (The framework will take care of closing the
// old cursor once we return.)
mAdapter.swapCursor(data);
}
}
public void onLoaderReset(Loader<Cursor> loader) {
// This is called when the last Cursor provided to onLoadFinished()
// above is about to be closed. We need to make sure we are no
// longer using it.
mAdapter.swapCursor(null);
} }
} }
//END_INCLUDE(fragment_cursor)
} }