[英]Populating a ListView from a SQLite database in Android
I am trying to populate a ListView in a separate class with data taken from a SQLite database held in another class. 我试图用从另一个类中保存的SQLite数据库获取的数据填充一个单独的类中的ListView。 What would be the easiest way to do so? 这样做最简单的方法是什么?
Thanks 谢谢
Return a Cursor eg: getList()
from your other class to the current ListView class. 从另一个类返回一个游标,例如: 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);
in class you have a method returning Cursor 在课堂上,你有一个返回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.