jeudi 30 avril 2015

Displaying items in listView from sqlite database (Android)

I am trying to populate a list view on android. I've written the code and it runs without errors, however it crashes when i try to run it. Below is the Populate list view method:

 private void populateListView() {
    Cursor cursor = db.getAllRows();
    String[] fromFieldNames = new String[]{MySQLiteHelper.KEY_TITLE, MySQLiteHelper.KEY_AUTHOR};
    int[] toViewIDs = new int[]{R.id.textView2, R.id.textView3};
    SimpleCursorAdapter myCursorAdapter;
    myCursorAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.itemlayout,cursor,fromFieldNames,toViewIDs,0);
    listView.setAdapter(myCursorAdapter);
}

Here is the getAllRows function from my SQLiteHelper class

 public Cursor getAllRows()
{
    SQLiteDatabase db = this.getReadableDatabase();
    String query = "SELECT * FROM " + TABLE_BOOKS;
    Cursor cursor = db.rawQuery(query, null);
    if(cursor!=null)
    {
       cursor.moveToFirst();
    }
   return cursor;
}

From the crash report, the error seems to be from :

 myCursorAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.itemlayout,cursor,fromFieldNames,toViewIDs,0);

I cannot seem to figure out what the problem is. Thank you

Aucun commentaire:

Enregistrer un commentaire