14.03.04
數據庫適配器
遊標適配器cursorAdapter
Newview:創建行佈局對象
Bindview:更新數據
Id默認必須爲_id
private void show() { Cursor cursor = mDb.query("students", null, null, null, null, null, null); String[] from=new String[]{"name","phone"}; int[] to=new int[]{R.id.textView1,R.id.textView2}; SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item_list, cursor, from, to); listview.setAdapter(adapter); }
若任然報錯,將數據庫表刪除
適配器聲明爲成員變量,則添加數據後無顯示
添加顯示:
重新遍歷遊標
private void show() { Cursor cursor = mDb.query("students", null, null, null, null, null, null); String[] from=new String[]{"name","phone"}; int[] to=new int[]{R.id.textView1,R.id.textView2}; SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item_list, cursor, from, to); listview.setAdapter(adapter); }
simpleCursorAdapter:簡單遊標適配器
顯示
優:代碼結構簡單
缺:支持的控件有限
textview、imageview、radiobutton、checkbox(CompoundButton)
private void show() { Cursor cursor = mDb.query("students", null, null, null, null, null, null); String[] from=new String[]{"name","phone"}; int[] to=new int[]{R.id.textView1,R.id.textView2}; SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item_list, cursor, from, to); listview.setAdapter(adapter); }