Android从SQLite List中填充Listview

时间:2015-07-21 07:30:47

标签: android sqlite listview android-listview

我能够填充我的SQLite数据库并执行所有CRUD操作。 现在我将从:

开始填充listview
List<Starred> data = DBAdapter.getAllUserData();

按日志,我可以阅读我的清单:

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

我很确定我错过了适配器,但不知道如何开始。

这是我的getAllUserData()功能:

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;

我试过

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);

但我得到了nullpointer异常。 有人能给我建议吗?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)