[英]Populating a ListView from a SQLite database in Android
我試圖用從另一個類中保存的SQLite數據庫獲取的數據填充一個單獨的類中的ListView。 這樣做最簡單的方法是什么?
謝謝
從另一個類返回一個游標,例如: getList()
到當前的ListView類。
objItem = new Contacts(this);
this.cur = objItem.getList();
this.startManagingCursor(this.cur);
ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cur,
new String[] { ContactsContract.Contacts.DISPLAY_NAME}, new int[] { android.R.id.text1});
setListAdapter(adapter);
在課堂上,你有一個返回Cursor的方法
public Cursor getList() {
// Get the base URI for the People table in the Contacts content
// provider.
Uri contacts = ContactsContract.Contacts.CONTENT_URI;
// Make the query.
ContentResolver cr = ctx.getContentResolver();
// Form an array specifying which columns to return.
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME };
Cursor managedCursor = cr.query(contacts, projection, null, null,
ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC");
return managedCursor;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.