从具有ID,Android的联系人提供商中检索电话号码

从具有ID,Android的联系人提供商中检索电话号码,第1张

概述我可以检索联系人ID,但是稍后我希望根据联系人ID分别检索电话号码.下面的代码返回电话号码的空结果.(我确实希望稍后再检索名称和电话号码并填充视图,但是我只是想让电话号码首先起作用).在我的onCreate中,我有此代码StringphoneNum=getPhoneNumber(myID);TextViewp

我可以检索联系人ID,但是稍后我希望根据联系人ID分别检索电话号码.下面的代码返回电话号码的空结果. (我确实希望稍后再检索名称和电话号码并填充视图,但是我只是想让电话号码首先起作用).

在我的onCreate中,我有此代码

   String phoneNum = getPhoneNumber(myID);   TextVIEw phoneTextVIEw = (TextVIEw) findVIEwByID(R.ID.textVIEwPhone);   phoneTextVIEw.setText(phoneNum);

这是getPhoneNumber()的方法

    protected String getPhoneNumber(String ID) {    ArrayList<String> phones = new ArrayList<String>();    Cursor cursor = getContentResolver().query(            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,            null,            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",            new String[]{ID}, null);    while (cursor.movetoNext()) {        phones.add(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));    }    cursor.close();    String phoneNum;    phoneNum = phones.get(0);    return phoneNum;}//end getPhoneNumber();

}

这将产生错误java.lang.indexoutofboundsexception:无效的索引0,大小为0,我打算为其创建一些错误处理.但是,我仍然确定我具有前面代码中的ID,所以我不知道为什么ArrayList返回null.如果您想查看该代码,它也位于我的onCreate中:

    Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);            if (cursor.getCount() != 0) {                int numContacts = cursor.getCount();                ArrayList<String> IDList = new ArrayList<>();                Random rand = new Random();                int randomNum = rand.nextInt(numContacts);                while (cursor.movetoNext()) {                    String ID = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));                    IDList.add(ID);                }                myID = IDList.get(randomNum);                String myString = Integer.toString(randomNum);                TextVIEw myTextVIEw = (TextVIEw) findVIEwByID(R.ID.textVIEwID);                myTextVIEw.setText(myString);                if (myID != null) {                    myTextVIEw.setText(myID);                } else {                    myTextVIEw.setText("Try Again!");                }            } else {                Toast.makeText(getApplicationContext(), "Your have no contacts.", Toast.LENGTH_SHORT).show();            }            cursor.close();

解决方法:

// You can fetch the Contact Number and Email With Following Methods.String phone = getPhoneNumber(ContactID);String email = getEmail("" + ContactID);private String getPhoneNumber(long ID) {        String phone = null;        Cursor phonesCursor = null;        phonesCursor = queryPhoneNumbers(ID);        if (phonesCursor == null || phonesCursor.getCount() == 0) {            // No valID number            //signalError();            return null;        } else if (phonesCursor.getCount() == 1) {            // only one number, call it.            phone = phonesCursor.getString(phonesCursor                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));        } else {            phonesCursor.movetoposition(-1);            while (phonesCursor.movetoNext()) {                // Found super primary, call it.                phone = phonesCursor.getString(phonesCursor                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));                break;            }        }        return phone;    }    private Cursor queryPhoneNumbers(long contactID) {        ContentResolver cr = getContentResolver();        Uri baseUri = ContentUris.withAppendedID(ContactsContract.Contacts.CONTENT_URI,                contactID);        Uri dataUri = Uri.withAppendedpath(baseUri,                ContactsContract.Contacts.Data.CONTENT_DIRECTORY);        Cursor c = cr.query(dataUri, new String[]{ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.CommonDataKinds.Phone.NUMBER,                        ContactsContract.CommonDataKinds.Phone.IS_SUPER_PRIMARY, ContactsContract.RawContacts.ACCOUNT_TYPE,                        ContactsContract.CommonDataKinds.Phone.TYPE,                        ContactsContract.CommonDataKinds.Phone.LABEL},                ContactsContract.Data.MIMETYPE + "=?",                new String[]{ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE}, null);        if (c != null && c.movetoFirst()) {            return c;        }        return null;    }    private String getEmail(String ID) {        String email = "";        ContentResolver cr = getContentResolver();        Cursor emailCur = cr.query(                ContactsContract.CommonDataKinds.Email.CONTENT_URI,                null,                ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",                new String[]{ID}, null);        while (emailCur.movetoNext()) {            // This would allow you get several email addresses            // if the email addresses were stored in an array            email = emailCur.getString(                    emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));//          String emailType = emailCur.getString(//                  emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));        }        emailCur.close();        return email;    }

总结

以上是内存溢出为你收集整理的从具有ID,Android的联系人提供商中检索电话号码全部内容,希望文章能够帮你解决从具有ID,Android的联系人提供商中检索电话号码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1092991.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存