【发布时间】:2015-01-12 14:38:55
【问题描述】:
我正在尝试为我的应用创建一个ListView,但我得到了以下答案
java.lang.RuntimeException: 你的内容必须有一个 id 属性为 'android.R.id.list' 的 ListView
当我成功使ListView 工作时出现此问题,但在添加或删除数据 mysq sqlite 数据库后它不会更新。
我的ListView 代码;
MessageRepo repo = new MessageRepo(this);
msgsList = repo.getMessageList();
lv = getListView();
ArrayAdapter<HashMap<String, String>> arrayAdapter = new ArrayAdapter<HashMap<String, String>>(this,android.R.layout.simple_list_item_1, msgsList);
setListAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();//my second problem
getMessageList() 代码:
public ArrayList<HashMap<String, String>> getMessageList() {
SQLiteDatabase db = dbHelper.getReadableDatabase();
String selectQuery = "SELECT " +
Message.KEY_ID + "," +
Message.KEY_message +
" FROM " + Message.TABLE;
//Message Message = new Message();
ArrayList<HashMap<String, String>> MessageList = new ArrayList<HashMap<String, String>>();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
HashMap<String, String> Messagehash = new HashMap<String, String>();
Messagehash.put("id", cursor.getString(cursor.getColumnIndex(Message.KEY_ID)));
Messagehash.put("message", cursor.getString(cursor.getColumnIndex(Message.KEY_message)));
MessageList.add(Messagehash);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return MessageList;
}
列表视图xml代码:
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
提前致谢!
【问题讨论】:
标签: java android sqlite listview android-listview