vendredi 1 janvier 2016

SimpleCursorAdaptor alternative for displaying SQLite records in ListView

SimpleCursorAdaptor is deprecated. I found out it's not going to be ideal for me because it uses the UI thread and I will be displaying a few hundred records. I want to use something that uses a background thread. After some research, I came across CursorLoader. I have been looking at tutorials but I'm finding it really tricky to implement. If there is a better alternative to CursorLoader, let me know. Please could someone help me out. The code I have so far. is:

public void getListFromDb(){
    Cursor res = myDb.ViewAll();
    startManagingCursor(res);


    //Map cursor from db to viewFields
    String[] fromFieldNames = new String[]{DatabaseHelper.COL_2, DatabaseHelper.COL_3, DatabaseHelper.COL_4, DatabaseHelper.COL_5};
    int[] toViewIDS = new int[]{R.id.viewName, R.id.viewAddress, R.id.viewPostcode, R.id.viewType};

    //Create adaptor to map items from DB to UI
    SimpleCursorAdapter myCursorAdaptor = new SimpleCursorAdapter(this, R.id.item_layout, res, fromFieldNames, toViewIDS);

    // Set adaptor for listView
    ListView myList = (ListView) findViewById(R.id.listViewLocations);
    myList.setAdapter(myCursorAdaptor);

}

Aucun commentaire:

Enregistrer un commentaire