0
Cursor searchCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
        new String[] {"_id",Phone.DISPLAY_NAME}, Phone.DISPLAY_NAME + " like ?", 
        new String[]{ "%" + cc.get("contactName").toString() + "%"}, null);

startManagingCursor(searchCursor);
while(searchCursor.isAfterLast() == false) {
    final String name = searchCursor.getString(searchCursor.getColumnIndex(Phone.DISPLAY_NAME));
    final String number = searchCursor.getString(searchCursor.getColumnIndex(Phone.NUMBER));
    str =new String[]{name,number};
    ada = new SimpleCursorAdapter(this, R.layout.view_contacts_listview_layout, searchCursor, str, new int[] { R.id.contactName, R.id.contactPhoneNo });
}

lvSearch.setAdapter(ada);

游标查询运行良好仅在简单游标适配器中出现问题。

4

1 回答 1

1
str =new String[]{name,number};

应该

str = new String[]{Phone.DISPLAY_NAME, Phone.NUMBER};

您应该将列名传递给 SimpleCursorAdapter。相反,您将列值(例如 555-555-5555,“john”)作为要使用的列名传递

此外,您的代码可以简化为:

Cursor searchCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                new String[] {"_id",Phone.DISPLAY_NAME}, Phone.DISPLAY_NAME + " like ?", 
                 new String[]{ "%" + cc.get("contactName").toString() + "%"}, null);

startManagingCursor(searchCursor);
str = new String[]{Phone.DISPLAY_NAME, Phone.NUMBER};
ada = new SimpleCursorAdapter(this,
                                R.layout.view_contacts_listview_layout, searchCursor,
                                str, new int[] {
                                        R.id.contactName, R.id.contactPhoneNo });

在将光标发送到 SimpleCursorAdapter 之前,没有理由访问您的光标。它将自动管理您需要的一切。

我还注意到,尽管您尝试访问 SimpleCursorAdapter 中的 NUMBER,但您也只在查询中选择了联系人 _ID 和 DISPLAY_NAME。您应该修改投影以包含电话号码。例如:

String[] projection = new String[] { BaseColumns._ID, Phone.DISPLAY_NAME, Phone.NUMBER };

Cursor searchCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    projection , Phone.DISPLAY_NAME + " like ?", 
                     new String[]{ "%" + cc.get("contactName").toString() + "%"}, null);
于 2012-04-30T14:15:39.817 回答