-2

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.

Nonjoe
  • 121
  • 1
  • 3
  • 11
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Selvin Jul 21 '15 at 07:46
  • and free hint as there was bazillion similar question here: prolly lv is null, prolly becuase a) you did not set content view b) there is no ListView with id R.id.list on used layout ... just folow the my prev comment for debuging and delete this question ... – Selvin Jul 21 '15 at 07:47

1 Answers1

0

try to use the cursor adapter pal https://github.com/codepath/android_guides/wiki/Populating-a-ListView-with-a-CursorAdapter

george kapoya
  • 21
  • 1
  • 3