2010-03-25 126 views

回答

4

返回游标如: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; 
    } 
+0

再次感谢Pentium10,将我能够使用此代码,即使我还没有实现内容提供商? – jcrowson 2010-03-25 13:33:34

+0

我刚刚复制粘贴这个。你使用'Cursor cur = db.query(...)'方法并返回Cursor变量。 – Pentium10 2010-03-25 13:49:37

+0

@ Pentium10你可以解决这个问题,我需要帮助http://stackoverflow.com/questions/38435531/android-setting-sqlite-database-values-in-an-listview – 2016-07-18 12:20:38