0

I have some problems with displaying table content in listview.

I have this method:

public ArrayList<category> getAllCategories()
{
    ArrayList<category> categories = new ArrayList<category>();

    String selectQuery = "SELECT * FROM " + category.TABLE;

    Log.e("DBHelper",selectQuery);

    SQLiteDatabase db = dbHelper.getReadableDatabase();

    Cursor c = db.rawQuery(selectQuery,null);


    if(c.moveToFirst())
    {
        do{
            category cat = new category();
            cat.set_id(c.getInt(c.getColumnIndex(category.KEY_ID)));
            cat.set_category_name(c.getString(c.getColumnIndex(category.KEY_CATEGORY_NAME)));
            categories.add(cat);
        } while(c.moveToNext());
    }

    return categories;

}

and I want to call this method in mainactivity.java and see result in listview, but I don't know why this code doesn't work:

    categoryRepo categoryRepo = new categoryRepo(this);
    ArrayAdapter<String> adapter;
    ListView list;
    list = (ListView) findViewById(R.id.listView1);

    ArrayList<category> categories = categoryRepo.getAllCategories();

    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice,categories); // --here I get an error, cannot resolve constructor

    list.setAdapter(adapter);
ktos1234
  • 197
  • 1
  • 6
  • 22
  • use `SimpleCursaorAdapter`, not `ArrayAdapter` – pskink Nov 24 '16 at 19:33
  • I'm sorry but I don't get it how to use constructor for SimpleCursorAdapter. It takes different argument, so should I change my method GetAllCategories and return adapter?? – ktos1234 Nov 24 '16 at 20:53
  • `GetAllCategories` should return a `Cursor`, so just `return db.rawQuery(selectQuery,null);` – pskink Nov 24 '16 at 21:32
  • Can you just show me how? I tried to do it like it's shown here: http://stackoverflow.com/questions/12077955/android-using-simplecursoradapter-to-get-data-from-database-to-listview but I got error that SimpleCursorAdapter is deprecated. – ktos1234 Nov 24 '16 at 23:48
  • use non deprecated constructor then – pskink Nov 25 '16 at 05:22
  • I think I can't use it because of my API version. Is there any way to use ArrayAdapter? It seems easier to use. – ktos1234 Nov 25 '16 at 08:24
  • no, it does not seem, with `SCA` you have all in 4 lines of code, if API 11 is to new use `android.support.v4.widget.SimpleCursorAdapter` instead – pskink Nov 25 '16 at 08:28

0 Answers0