获取光标IndexOutOfBound传递contact-id以从Contacts中获取详细信息时出现异常

时间:2017-08-24 06:51:28

标签: android android-contentprovider contact rawcontacts

我根据帐号名称获得了一个人的联系方式,但工作正常但我得到了那些有姓名而不是号码的联系人的例外情况。我也得到那些联系人ID,但是当我使用来自Phone游标的contact_id获取详细信息时会导致异常

  private void displayAllContactsByType(String accountName) //account-name: Mobikwik
 {

    Cursor rawCursor = null;

    rawCursor = cResolver.query(
            ContactsContract.RawContacts.CONTENT_URI,
            new String[]{ContactsContract.RawContacts.CONTACT_ID},
            ContactsContract.RawContacts.ACCOUNT_NAME + "= ? AND "+ContactsContract.RawContacts.CONTACT_ID + " != 0",
            new String[]{accountName},
            ContactsContract.Contacts.DISPLAY_NAME_PRIMARY + " COLLATE LOCALIZED ASC");


    int contactIdColumn = rawCursor.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID);
    int rawCursorCount = rawCursor.getCount();
    int total = 1;

    Utils.Log("Raw Size", " " + rawCursorCount);

    while (rawCursor.moveToNext()) {

        contactId = rawCursor.getLong(contactIdColumn);
            storeContactDetails(contactId );
    }
    rawCursor.close();
}

从上面的方法我得到联系人ID,用于获取以下方法中的数据。

private void storeContactDetails( Long contactId) //e.g contact_id-2512 
{

    Cursor phones = null;
    String[] projection = new String[]{
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.NUMBER,
            ContactsContract.Contacts.LOOKUP_KEY,
            Phone.HAS_PHONE_NUMBER,
            Phone.TYPE,
            Phone.LAST_TIME_USED
    };


    phones = cResolver.query(Phone.CONTENT_URI,
            projection,
            Phone.CONTACT_ID + " = ? ",
            new String[]{String.valueOf(contactId)},
            null);

    phones.moveToFirst();
    getResultsFromPhoneCursor(phones);

}

public void getResultsFromPhoneCursor(Cursor phones) {

    String email_Id = "";
    String contactType = "";
    String lastTimeUsed = "";

    HashMap<String,String> contactTypeWithNumber=new HashMap<>();

    try {
        String hasPhone = "";
        display_name = "";
        phoneNumber = "";

        display_name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));// THIS LINE IS CAUSING EXCEPTION HERE
        lastTimeUsed = convertLongToDateFormat(phones.getString(phones.getColumnIndex(Phone.LAST_TIME_USED)));

        hasPhone = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER));

        if (hasPhone.equalsIgnoreCase("1"))
            hasPhone = "true";

        else
            hasPhone = "false";

        if (Boolean.parseBoolean(hasPhone)) {

            String lookupKey = phones.getString(phones.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));

            Cursor emails = cResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, new String[]{ContactsContract.CommonDataKinds.Email.DATA},
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null);

            while (emails.moveToNext()) {
                email_Id = emails.getString(emails
                        .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
            }

            emails.close();

            do {

                phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));

                switch (type) {

                    case 0:

                        contactType = "whatsapp";
                        break;

                    case Phone.TYPE_HOME:
                        contactType = "home";
                        break;

                    case Phone.TYPE_MOBILE:
                        contactType = "mobile";
                        break;

                    case Phone.TYPE_WORK:
                        contactType = "work";
                        break;

                    case Phone.TYPE_OTHER:
                        contactType = "other";
                        break;

                    case Phone.TYPE_WORK_PAGER:
                        contactType = "pager";
                        break;

                    case Phone.TYPE_MMS:
                        contactType = "mms";
                        break;

                    case Phone.TYPE_MAIN:
                        contactType = "main";
                        break;

                    case Phone.TYPE_FAX_HOME:
                        contactType = "fax";
                        break;

                    case Phone.TYPE_FAX_WORK:
                        contactType = "fax-home";
                        break;
                }

                if(contactList.contains(new ContactsWrapper(contactId, display_name, lookupKey, false, email_Id, lastTimeUsed,null))) {

                    try {
                        HashMap<String,String>contactTypeMapPrevious=contactList.get(contactList.size()-1).getContactTypeWithNumber();

                            if (!contactTypeMapPrevious.get(contactType).replace(" ","").equals(this.phoneNumber.replace(" ",""))) {
                                contactTypeWithNumber.put(contactType, phoneNumber);

                            }

                            else
                            {
                                if(!contactTypeWithNumber.containsKey(contactType)&& contactTypeMapPrevious.size()<=1)
                                    contactTypeWithNumber.put(contactType, phoneNumber);
                            }


                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                else
                    contactTypeWithNumber.put(contactType,phoneNumber);
            }

            while (phones.moveToNext());
            contactList.add(new ContactsWrapper(contactId, display_name, lookupKey, false, email_Id, lastTimeUsed,contactTypeWithNumber));


            phones.close();
        }
    } catch (Exception e) {

        e.printStackTrace();//Cursor Out of Bound Exception here
    }

}

为什么即使我的联系人ID不是0或null,我也会收到此异常。此代码适用于同时具有姓名和号码的联系人请提前帮助谢谢

0 个答案:

没有答案