我正在尝试使用从另一个类中保存的SQLite数据库中获取的数据在单独的类中填充ListView。最简单的方法是什么?
由于
答案 0 :(得分:4)
将一个Cursor例如: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);
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;
}