Update to use new LoaderManager API.
Change-Id: I76c7e7210979742e4ab2140ee68fcaf2207505e5
This commit is contained in:
@@ -34,9 +34,11 @@
|
|||||||
|
|
||||||
<!-- We will request access to the camera, saying we require a camera
|
<!-- We will request access to the camera, saying we require a camera
|
||||||
of some sort but not one with autofocus capability. -->
|
of some sort but not one with autofocus capability. -->
|
||||||
|
<!--
|
||||||
<uses-permission android:name="android.permission.CAMERA" />
|
<uses-permission android:name="android.permission.CAMERA" />
|
||||||
<uses-feature android:name="android.hardware.camera" />
|
<uses-feature android:name="android.hardware.camera" />
|
||||||
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
|
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
|
||||||
|
-->
|
||||||
|
|
||||||
<application android:name="ApiDemosApplication"
|
<application android:name="ApiDemosApplication"
|
||||||
android:label="@string/activity_sample_code"
|
android:label="@string/activity_sample_code"
|
||||||
|
|||||||
@@ -16,22 +16,16 @@
|
|||||||
|
|
||||||
package com.example.android.apis.app;
|
package com.example.android.apis.app;
|
||||||
|
|
||||||
import com.example.android.apis.Shakespeare;
|
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.Fragment;
|
|
||||||
import android.app.ListFragment;
|
import android.app.ListFragment;
|
||||||
import android.app.LoaderManagingFragment;
|
import android.app.LoaderManager;
|
||||||
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.Contacts.Phones;
|
import android.provider.Contacts.Phones;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.ArrayAdapter;
|
|
||||||
import android.widget.ListAdapter;
|
import android.widget.ListAdapter;
|
||||||
import android.widget.ListView;
|
import android.widget.ListView;
|
||||||
import android.widget.SimpleCursorAdapter;
|
import android.widget.SimpleCursorAdapter;
|
||||||
@@ -52,30 +46,8 @@ public class FragmentComplexList extends Activity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class ContactsLoader extends LoaderManagingFragment<Cursor> {
|
public static class ExampleComplexListFragment extends ListFragment
|
||||||
ExampleComplexListFragment mListFragment;
|
implements LoaderManager.LoaderCallbacks<Cursor> {
|
||||||
|
|
||||||
void setListFragment(ExampleComplexListFragment fragment) {
|
|
||||||
mListFragment = fragment;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
|
||||||
return new CursorLoader(getActivity(), Phones.CONTENT_URI, null, null, null, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onInitializeLoaders() {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onLoadFinished(Loader<Cursor> loader, Cursor data) {
|
|
||||||
mListFragment.loadFinished(loader, data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class ExampleComplexListFragment extends ListFragment {
|
|
||||||
ContactsLoader mLoader;
|
|
||||||
boolean mInitializing;
|
boolean mInitializing;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -83,21 +55,11 @@ public class FragmentComplexList extends Activity {
|
|||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
mInitializing = true;
|
mInitializing = true;
|
||||||
|
|
||||||
// Make sure we have the loader for our content.
|
|
||||||
mLoader = (ContactsLoader)getActivity().findFragmentByTag(
|
|
||||||
ContactsLoader.class.getName());
|
|
||||||
if (mLoader == null) {
|
|
||||||
mLoader = new ContactsLoader();
|
|
||||||
getActivity().openFragmentTransaction().add(mLoader,
|
|
||||||
ContactsLoader.class.getName()).commit();
|
|
||||||
}
|
|
||||||
mLoader.setListFragment(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onReady(Bundle savedInstanceState) {
|
public void onActivityCreated(Bundle savedInstanceState) {
|
||||||
super.onReady(savedInstanceState);
|
super.onActivityCreated(savedInstanceState);
|
||||||
|
|
||||||
// Assume we don't yet have data to display.
|
// Assume we don't yet have data to display.
|
||||||
setListShown(false, false);
|
setListShown(false, false);
|
||||||
@@ -107,10 +69,10 @@ public class FragmentComplexList extends Activity {
|
|||||||
setEmptyText("No phone numbers");
|
setEmptyText("No phone numbers");
|
||||||
|
|
||||||
// Check if we already have content for the list.
|
// Check if we already have content for the list.
|
||||||
Loader<Cursor> ld = mLoader.getLoader(0);
|
Loader<Cursor> ld = getLoaderManager().getLoader(0);
|
||||||
if (ld == null) {
|
if (ld == null) {
|
||||||
// No loader started yet... do it now.
|
// No loader started yet... do it now.
|
||||||
ld = mLoader.startLoading(0, null);
|
ld = getLoaderManager().startLoading(0, null, this);
|
||||||
} else {
|
} else {
|
||||||
// Already have a loader -- poke it to report its cursor
|
// Already have a loader -- poke it to report its cursor
|
||||||
// if it already has one. This will immediately call back
|
// if it already has one. This will immediately call back
|
||||||
@@ -120,15 +82,6 @@ public class FragmentComplexList extends Activity {
|
|||||||
mInitializing = false;
|
mInitializing = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void loadFinished(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 int[] { android.R.id.text1, android.R.id.text2 });
|
|
||||||
setListAdapter(adapter);
|
|
||||||
setListShown(true, !mInitializing);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onListItemClick(ListView l, View v, int position, long id) {
|
public void onListItemClick(ListView l, View v, int position, long id) {
|
||||||
Log.i("FragmentComplexList", "Item clicked: " + id);
|
Log.i("FragmentComplexList", "Item clicked: " + id);
|
||||||
@@ -136,8 +89,22 @@ public class FragmentComplexList extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
mLoader.setListFragment(this);
|
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
||||||
|
return new CursorLoader(getActivity(), Phones.CONTENT_URI, null, null, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@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 int[] { android.R.id.text1, android.R.id.text2 });
|
||||||
|
setListAdapter(adapter);
|
||||||
|
setListShown(true, !mInitializing);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,8 +43,8 @@ public class FragmentList extends Activity {
|
|||||||
|
|
||||||
public static class ExampleListFragment extends ListFragment {
|
public static class ExampleListFragment extends ListFragment {
|
||||||
@Override
|
@Override
|
||||||
public void onReady(Bundle savedInstanceState) {
|
public void onActivityCreated(Bundle savedInstanceState) {
|
||||||
super.onReady(savedInstanceState);
|
super.onActivityCreated(savedInstanceState);
|
||||||
setListAdapter(new ArrayAdapter<String>(getActivity(),
|
setListAdapter(new ArrayAdapter<String>(getActivity(),
|
||||||
android.R.layout.simple_list_item_1, Shakespeare.TITLES));
|
android.R.layout.simple_list_item_1, Shakespeare.TITLES));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -143,8 +143,8 @@ public class FragmentRetainInstance extends Activity {
|
|||||||
* to a new activity.
|
* to a new activity.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void onReady(Bundle savedInstanceState) {
|
public void onActivityCreated(Bundle savedInstanceState) {
|
||||||
super.onReady(savedInstanceState);
|
super.onActivityCreated(savedInstanceState);
|
||||||
|
|
||||||
// Retrieve the progress bar from the current activity.
|
// Retrieve the progress bar from the current activity.
|
||||||
mProgressBar = (ProgressBar)getActivity().findViewById(
|
mProgressBar = (ProgressBar)getActivity().findViewById(
|
||||||
|
|||||||
Reference in New Issue
Block a user