Java 在Android中从SQLite数据库填充ListView
我试图用另一个类中的SQLite数据库中的数据填充另一个类中的ListView。最简单的方法是什么Java 在Android中从SQLite数据库填充ListView,java,android,sqlite,listview,Java,Android,Sqlite,Listview,我试图用另一个类中的SQLite数据库中的数据填充另一个类中的ListView。最简单的方法是什么 谢谢将光标例如:getList()从您的其他类返回到当前的ListView类 objItem = new Contacts(this); this.cur = objItem.getList(); this.startManagingCursor(this.cur); ListAdapter adapter = new SimpleCursorAdapter(this, android.R.l
谢谢将光标例如:
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;
}
再次感谢奔腾10,即使我没有实现内容提供商,我也能使用这段代码吗?我刚刚复制粘贴了这段代码。您可以使用
Cursor cur=db.query(…)
方法并返回Cursor变量。@奔腾10可以解决这个问题吗?我需要帮助