0

我在 Android 2.2 中制作了一个应用程序,它只在listview. 所有电话簿、联系人和号码都ListView带有CheckBox。当我在 Emulator 2.2 中运行应用程序时,它可以正常工作,但是当我使用真实设备测试应用程序时,它只会显示联系人电子邮件 ID 以及何时CheckBoxes使用应用程序崩溃。

我想清楚我不会从任何地方调用电子邮件 ID。我已经使用下面的代码来调用从电话簿中获取联系人详细信息。

在 BaseAdapter 类中:

             ContentResolver cr = getContentResolver();
             cr = context.getContentResolver();
         cursor = cr.query(
                ContactsContract.Contacts.CONTENT_URI,
                null,
                null,
                null,
                null);


public View getView(final int position, View convertView, ViewGroup parentView) 
{

                    if(cursor != null){
             while(cursor.moveToNext()){
                    int nameFiledColumnIndex = cursor.getColumnIndex(
                                    ContactsContract.Contacts.DISPLAY_NAME);
                    String id = cursor.getString(
                            cursor.getColumnIndex(ContactsContract.Contacts._ID));
                    Cursor pCur = cr.query(
                         ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                         null, 
                         ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                         new String[]{id}, null);   

                    while (pCur.moveToNext()) {
                     phoneNo = pCur.getString(pCur.getColumnIndex(
                             ContactsContract.CommonDataKinds.Phone.NUMBER));

    }

有人知道我犯了什么错误吗?

4

1 回答 1

0

试试这个...

     ContentResolver contentResolver = getContentResolver();
    Cursor cur = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null,
            null, null, null);

    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur
                    .getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur
                    .getString(cur
                            .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer
                    .parseInt(cur.getString(cur
                            .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                // Query phone here. Covered next

                Cursor phoneCur = contentResolver.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = ?", new String[] { id }, null);
                while (phoneCur.moveToNext()) {
                    // Do something with phones
                    String phoneNo = phoneCur.getString(pCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                    contactDataset = new ContactDataset(); //This is object of dataset class to add the contacts.
                    contactDataset.setName(name);
                    contactDataset.setPhoneNumber(phoneNo);
                    getContactList.add(contactDataset);                     
                }
                pCur.close();
            }
        }
    }

让我知道它是否有效.......

于 2012-08-10T06:28:57.763 回答