android Adapter筆記

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:簡單遊標適配器

顯示


優:代碼結構簡單

缺:支持的控件有限

textviewimageviewradiobuttoncheckboxCompoundButton


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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章