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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex