【发布时间】:2018-04-24 13:46:21
【问题描述】:
嗨,我使用 sqlite,我需要显示所有库名,但列表视图只显示最后一个列名检查图片enter image description here One is last added
private void ListShow() { list = findViewById(R.id.list); myDb.getReadableDatabase(); myDb.getWritableDatabase(); Cursor res = myDb.getAllData(); while (res.moveToNext()) { String[] strNAME = new String[]{res.getString(0) }; ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, strNAME); list.setAdapter(adapter); adapter.notifyDataSetChanged(); } }
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
return res;
}
【问题讨论】:
-
因为您在每次循环迭代时都在重新创建并重新分配 ArrayAdaper。另请注意,您误用了
notifyDatabaseChanged()方法
标签: android database sqlite listview android-sqlite