Search code examples
androidparse-platformandroid-listfragment

Populating a ListFragment with database data


I have a ListFragment and the behavior I want is to enter the fragment and see it filled with data from a database that I have. The hard part is that I would also like this Fragment to know when a new row is added to the database and show it.

Currently the only solution I have is to perform an update at a certain interval within the fragment, but I would like to know if there is a more effective solution and how it could be done.


Solution

  • You can use a CursorLoader for this. It assumes also that you have a ContentProvider as your data source for the application, which I will not discuss here but you can learn about them in this post if you want.

    To sync the database data to your ListView, you will have to do two things:

    • Create a custom Adapter that implements CursorAdapter
    • Implement a LoaderManager in your Fragment

    To implement the LoaderManager just override the three required methods. One is used to setup your Cursor, one will feed the results to your Adapter. From then on, assuming your ContentProvider properly notifies any URIs of changes, you will notice those changes in the Adapter. Here is a snippet of the Loader code:

    public class AccountsFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>{
        private static final int MY_LOADER = 0;
    
        @Override
        public void onActivityCreated() {
            super.onActivityCreated();
            getLoaderManager.initLoader(MY_LOADER, null, this);
        }
    
        @Override
        public Loader<Cursor> onCreateLoader(int id, Bundle args) {
            switch(id){
                case MY_LOADER:
                    return new CursorLoader(
                            getActivity(),
                            MY_URI,
                            MY_COLUMNS,
                            null,
                            null,
                            null
                    );
                default:
                    throw new UnsupportedOperationException("Unknown loader id: " + id);
            }
        }
    
        @Override
        public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
            switch(loader.getId()){
                case MY_LOADER:
                    mAdapter.swapCursor(datA);
                default:
                    throw new UnsupportedOperationException("Unknown loader id: " + loader.getId());
            }
        }
    
        @Override
        public void onLoaderReset(Loader<Cursor> loader) {
            switch(loader.getId()){
                case MY_LOADER:
                    mAdapter.swapCursor(null);
                    break;
                default:
                    throw new UnsupportedOperationException("Unknown loader id: " + loader.getId());
            }
        }
    
    }
    

    I learned much of this from a Udacity course that teaches you a lot about Android from the bottom up, and if you are looking for a complete overview I think it is very much worth your time.