联系ListView在仿真器中工作但不在真实设备中

时间:2012-08-09 12:05:19

标签: android

我在Android 2.2中创建了一个仅在listview中显示三个内容的应用程序。 ListViewCheckBox的所有电话簿,通讯录和号码。当我在模拟器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));

    }

有没有人知道我犯过什么错误?

1 个答案:

答案 0 :(得分: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();
            }
        }
    }

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