Search code examples
androiddatabasesqlitelistviewandroid-cursoradapter

CursorAdapter can't be instantiated !


I have a DataBaseHelper which has a fetchData() method that returns a Cursor containing a whole table rows drom an .sqlite file in assest folder.

I wrote this:

    try {
        Cursor cursor = myDbHelper.fetchData("tableName");
        txt.setText("Done!"); //for check only

    } catch (Exception e){
        txt.setText("Can't fetch data !");
    }

I added these two lines at the end of the try block. I did this in order to attach a CursorAdapter ca to a ListView lv :

        CursorAdapter ca = new CursorAdapter(getBaseContext(), cursor, false);
        lv.setAdapter(ca);

However, I got an error message at the CursorAdapter instantiation line:

Cannot instantiate the type CursorAdapter

What is wrong here ? am I using the wrong adapter to fetch from a database?


Solution

  • Hmm.... let's see: First we open reference page for CursorAdapter.

    Then we look at header, and we can read: public abstract class CursorAdapter Abstract classes can't be instantiated in Java (and in any other language implementing that concept) it's feature not a bug. Few lines more we can find link to SimpleCursorAdapter which is not abstract. So simple solution is "just use SimpleCustomAdapter instead of abstract CursorAdapter.

    SimpleCursorAdapter exists in Android API since Android 1.0 (API lvl1).