Skip to main content

Best practices to query SQLite database in ListFragment with CursorLoader?



I'm using Android Compatibility Library in my project. I've set up ListFragment as described in the DevGuide ( http://developer.android.com/reference/android/app/Fragment.html ), and using a simple CursorLoader Christian made be used without content provider ( Usage CursorLoader without ContentProvider ).





The question is where, in my ListFragment / parent Activity, should I open database, return the Cursor, create Adapter and setListAdapter?





So in my app, I have TitlesFragment, DetailsFragment, FragmentLayoutActivity, DetailsLayoutActivity.





Is the best practice...









  • to open database in ListFragment's onActivityCreated and close it in ListFragment's onDestroy like in code sample below







    @Override

    public void onActivityCreated(Bundle savedInstanceState) {

    super.onActivityCreated(savedInstanceState);

    // Open database

    playersDatabaseHelper = new PlayersDBAdapter(getActivity());

    playersDatabaseHelper.open();

    getLoaderManager().initLoader(0, null, this);

    ...

    }



    @Override

    public void onDestroy() {

    super.onDestroy();

    if (playersDatabaseHelper != null) {

    playersDatabaseHelper.close();

    }

    }











  • query database and return the cursor in onCreateLoader , and create the Adapter and setListAdapter in onLoadFinished like in code sample below







    @Override

    public Loader<Cursor> onCreateLoader(int id, Bundle args) {

    // Now create and return a CursorLoader that will take care of

    // creating a Cursor for the data being displayed.

    return new MyCursorLoader(getActivity()) {

    @Override

    public Cursor loadInBackground() {

    playersCursor = playersDatabaseHelper.getAllPlayers();

    return playersCursor;

    }

    };



    }



    @Override

    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {

    // Create an empty adapter we will use to display the loaded data.

    playersAdapter = new RowAdapter(getActivity(), playersCursor, R.layout.players_overview_row);



    // Allocate the adapter to the List displayed within this fragment.

    setListAdapter(playersAdapter);



    playersAdapter.swapCursor(cursor);



    // The list should now be shown.

    if (isResumed()) {

    setListShown(true);

    } else {

    setListShownNoAnimation(true);

    }

    }











Am I on the right track or should I move some of those somewhere? Thanks for your time!



Source: Tips4all

Comments

  1. Sorry no experience in CursorLoader yet and Fragment, but I already experienced use of SQLiteOpenHelper in the context of concurrent access by different threads & activities.

    I will assume that PlayersDBAdapter is internally using a SQLiteOpenHelper class. but it is not clear what your methods open() and close() are doing?

    What I did:


    define your SQLiteOpenHelper as an application wide singleton, not activity wide as you seem to do
    instantiate SQLiteOpenHelper single instance in your Application onCreate
    DON'T release SQLiteOpenHelper instance in any activity onDestroy, as when an activity stops, another one might still have to open the DB
    I guess SQLiteOpenHelper instance should be cleared in application onTerminate (not sure as onTerminate is in practice almost never called)
    I have DBAdapter object which gets a SQLiteDatabase reference with mySQLiteOpenHelper.getWritableDatabase()
    these DBAdapter are typically allocated in activity onCreate and released in onDestroy


    At least this works, no crashs in an application with several thousands users.
    Suggestions to improve that are welcome :-)

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 1 Final Exam 2011 latest (hot hot hot)

  Hi! I have been posted content of ccna1 final exam (latest and only question.) I will post the answer and insert image on sunday. If you care, please subscribe your email an become a first person have full test content. Subcribe now  Some question  have not content because this question have images content. So that can you wait for me? SUNDAY 1. A user sees the command prompt: Router(config-if)# . What task can be performed at this mode? Reload the device. Perform basic tests. Configure individual interfaces. Configure individual terminal lines. 2. Refer to the exhibit. Host A attempts to establish a TCP/IP session with host C. During this attempt, a frame was captured with the source MAC address 0050.7320.D632 and the destination MAC address 0030.8517.44C4. The packet inside the captured frame has an IP source address 192.168.7.5, and the destination IP address is 192.168.219.24. At which point in the network was this packet captured? leaving host A leaving ATL leaving...