Last active
September 15, 2015 12:11
-
-
Save SelvinPL/c6182418fd1c387bf5b6 to your computer and use it in GitHub Desktop.
Use CursorLoaders! It's easy! replace commented code with uncommented to get Phones(instead SMS) list in the same easy way
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package pl.selvin.simplelistfragmentsample; | |
| import android.database.Cursor; | |
| import android.net.Uri; | |
| import android.os.Bundle; | |
| import android.support.v4.app.FragmentActivity; | |
| import android.support.v4.app.ListFragment; | |
| import android.support.v4.app.LoaderManager; | |
| import android.support.v4.content.CursorLoader; | |
| import android.support.v4.content.Loader; | |
| import android.support.v4.widget.CursorAdapter; | |
| import android.support.v4.widget.SimpleCursorAdapter; | |
| import android.view.View; | |
| public class MainActivity extends FragmentActivity { | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| if (savedInstanceState == null) { | |
| getSupportFragmentManager().beginTransaction().replace(android.R.id.content, new SimpleListFragment()).commit(); | |
| } | |
| } | |
| public static class SimpleListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> { | |
| private final static int[] to = new int[]{android.R.id.text1, android.R.id.text2}; | |
| //private final static String[] from = new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER}; | |
| //private final static String[] projection = new String[]{ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER}; | |
| //private final static String orderBy = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME; | |
| //private final static Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; | |
| private final static String[] from = new String[]{"address", "body"}; | |
| private final static String[] projection = new String[]{"_id", "address", "body"}; | |
| private final static String orderBy = "address"; | |
| private final static Uri uri = Uri.parse("content://sms/inbox"); | |
| @Override | |
| public void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| final SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_2, null, from, to, 0); | |
| setListAdapter(adapter); | |
| } | |
| @Override | |
| public void onViewCreated(View view, Bundle savedInstanceState) { | |
| super.onViewCreated(view, savedInstanceState); | |
| setListShownNoAnimation(false); | |
| getListView().setFastScrollEnabled(true); | |
| getLoaderManager().initLoader(0, null, this); | |
| } | |
| @Override | |
| public Loader<Cursor> onCreateLoader(int i, Bundle bundle) { | |
| return new CursorLoader(getActivity(), uri, projection, null, null, orderBy) ; | |
| } | |
| @Override | |
| public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { | |
| ((CursorAdapter) getListAdapter()).swapCursor(cursor); | |
| if (isResumed()) { | |
| setListShown(true); | |
| } else { | |
| setListShownNoAnimation(true); | |
| } | |
| } | |
| @Override | |
| public void onLoaderReset(Loader<Cursor> loader) { | |
| ((CursorAdapter) getListAdapter()).swapCursor(null); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment