Update to follow LoaderManager API.
Also renaming the list samples to make it clearer what they are. Change-Id: I91b209a32ad4dbf82982682884769d1cde8e3bc3
This commit is contained in:
@@ -218,13 +218,6 @@
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<activity android:name=".app.FragmentComplexList" android:label="@string/fragment_complex_list">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.SAMPLE_CODE" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<activity android:name=".app.FragmentContextMenu"
|
||||
android:label="@string/fragment_context_menu">
|
||||
<intent-filter>
|
||||
@@ -242,7 +235,14 @@
|
||||
|
||||
<activity android:name=".app.FragmentLayout$DialogActivity" />
|
||||
|
||||
<activity android:name=".app.FragmentList" android:label="@string/fragment_list">
|
||||
<activity android:name=".app.FragmentListCursorLoader" android:label="@string/fragment_list_cursor_loader">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.SAMPLE_CODE" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<activity android:name=".app.FragmentListArray" android:label="@string/fragment_list_array">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.SAMPLE_CODE" />
|
||||
|
||||
@@ -85,8 +85,6 @@
|
||||
|
||||
<string name="fragment_anim">App/Fragment/Anim</string>
|
||||
|
||||
<string name="fragment_complex_list">App/Fragment/Complex List</string>
|
||||
|
||||
<string name="fragment_context_menu">App/Fragment/Context Menu</string>
|
||||
<string name="fragment_context_menu_msg">Fragment populating a context
|
||||
menu; long press the button to see.</string>
|
||||
@@ -94,7 +92,9 @@
|
||||
|
||||
<string name="fragment_layout">App/Fragment/Layout</string>
|
||||
|
||||
<string name="fragment_list">App/Fragment/List</string>
|
||||
<string name="fragment_list_array">App/Fragment/List Array</string>
|
||||
|
||||
<string name="fragment_list_cursor_loader">App/Fragment/List Cursor Loader</string>
|
||||
|
||||
<string name="fragment_menu">App/Fragment/Menu</string>
|
||||
<string name="fragment_menu_msg">Build menus from two fragments, allowing
|
||||
|
||||
@@ -27,21 +27,22 @@ import android.widget.ArrayAdapter;
|
||||
import android.widget.ListView;
|
||||
|
||||
/**
|
||||
* Demonstration of using ListFragment to show a list of items.
|
||||
* Demonstration of using ListFragment to show a list of items
|
||||
* from a canned array.
|
||||
*/
|
||||
public class FragmentList extends Activity {
|
||||
public class FragmentListArray extends Activity {
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
// Create the list fragment and add it as our sole content.
|
||||
if (findFragmentById(android.R.id.content) == null) {
|
||||
ExampleListFragment list = new ExampleListFragment();
|
||||
ArrayListFragment list = new ArrayListFragment();
|
||||
openFragmentTransaction().add(android.R.id.content, list).commit();
|
||||
}
|
||||
}
|
||||
|
||||
public static class ExampleListFragment extends ListFragment {
|
||||
public static class ArrayListFragment extends ListFragment {
|
||||
@Override
|
||||
public void onActivityCreated(Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
@@ -24,6 +24,7 @@ import android.content.Loader;
|
||||
import android.database.Cursor;
|
||||
import android.os.Bundle;
|
||||
import android.provider.Contacts.Phones;
|
||||
import android.provider.ContactsContract.Contacts;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.ListAdapter;
|
||||
@@ -34,29 +35,20 @@ import android.widget.SimpleCursorAdapter;
|
||||
* Demonstration of more complex use if a ListFragment, including showing
|
||||
* an empty view and loading progress.
|
||||
*/
|
||||
public class FragmentComplexList extends Activity {
|
||||
public class FragmentListCursorLoader extends Activity {
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
// Create the list fragment and add it as our sole content.
|
||||
if (findFragmentById(android.R.id.content) == null) {
|
||||
ExampleComplexListFragment list = new ExampleComplexListFragment();
|
||||
CursorLoaderListFragment list = new CursorLoaderListFragment();
|
||||
openFragmentTransaction().add(android.R.id.content, list).commit();
|
||||
}
|
||||
}
|
||||
|
||||
public static class ExampleComplexListFragment extends ListFragment
|
||||
public static class CursorLoaderListFragment extends ListFragment
|
||||
implements LoaderManager.LoaderCallbacks<Cursor> {
|
||||
boolean mInitializing;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
mInitializing = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
@@ -68,18 +60,9 @@ public class FragmentComplexList extends Activity {
|
||||
// application this would come from a resource.
|
||||
setEmptyText("No phone numbers");
|
||||
|
||||
// Check if we already have content for the list.
|
||||
Loader<Cursor> ld = getLoaderManager().getLoader(0);
|
||||
if (ld == null) {
|
||||
// No loader started yet... do it now.
|
||||
ld = getLoaderManager().startLoading(0, null, this);
|
||||
} else {
|
||||
// Already have a loader -- poke it to report its cursor
|
||||
// if it already has one. This will immediately call back
|
||||
// to us for us to update the list right now.
|
||||
ld.startLoading();
|
||||
}
|
||||
mInitializing = false;
|
||||
// Prepare the loader. Either re-connect with an existing one,
|
||||
// or start a new one.
|
||||
getLoaderManager().initLoader(0, null, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -92,19 +75,33 @@ public class FragmentComplexList extends Activity {
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
|
||||
Contacts._ID,
|
||||
Contacts.DISPLAY_NAME,
|
||||
Contacts.CONTACT_STATUS,
|
||||
Contacts.CONTACT_PRESENCE,
|
||||
Contacts.PHOTO_ID,
|
||||
Contacts.LOOKUP_KEY,
|
||||
};
|
||||
|
||||
@Override
|
||||
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
||||
return new CursorLoader(getActivity(), Phones.CONTENT_URI, null, null, null, null);
|
||||
String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
|
||||
+ Contacts.HAS_PHONE_NUMBER + "=1) AND ("
|
||||
+ Contacts.DISPLAY_NAME + " != '' ))";
|
||||
return new CursorLoader(getActivity(), Contacts.CONTENT_URI,
|
||||
CONTACTS_SUMMARY_PROJECTION, select, null,
|
||||
Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
|
||||
ListAdapter adapter = new SimpleCursorAdapter(getActivity(),
|
||||
android.R.layout.simple_list_item_2, data,
|
||||
new String[] { Phones.NAME, Phones.NUMBER },
|
||||
new String[] { Contacts.DISPLAY_NAME, Contacts.CONTACT_STATUS },
|
||||
new int[] { android.R.id.text1, android.R.id.text2 });
|
||||
setListAdapter(adapter);
|
||||
setListShown(true, !mInitializing);
|
||||
setListShown(true, isResumed());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user