mardi 21 juillet 2015

Android populate Listview from SQLite List

I'm able to populate my SQLite DB and to do all CRUD operations. Now I would populate a listview starting from:

List<Starred> data = DBAdapter.getAllUserData();

By log I can read my list:

for (Starred st : data) 
    Log.d("Title: ", "Title: " + st.getName());

I'm pretty sure I'm missing the Adapter but don't well know how start.

This is my getAllUserData() function:

public static List<Starred> getAllUserData() {

    List<Starred> starredList = new ArrayList<Starred>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + USER_TABLE;

    final SQLiteDatabase db = open();
    Cursor cursor = db.rawQuery ( selectQuery, null );

    if (cursor.moveToFirst()) {
        do {
            Starred data = new Starred();
            data.setID(Integer.parseInt(cursor.getString(0)));
            data.setName(cursor.getString(1));
            data.setLabel(cursor.getString(2));

            // Adding contact to list
            starredList.add(data);
        } while (cursor.moveToNext());
    }

    return starredList;

I tried with

ListView lv = (ListView) findViewById(R.id.list); ArrayAdapter<Starred> arrayAdapter = new ArrayAdapter<Starred>( this, android.R.layout.simple_list_item_1, data); lv.setAdapter(arrayAdapter);

but I get nullpointer exception. Could anyone give me suggestion? Any help would be much appreciated.

Aucun commentaire:

Enregistrer un commentaire