检索 Android 联系人

发布于 2024-12-16 16:32:04 字数 1160 浏览 6 评论 0原文

ContentResolver cr = getContentResolver();
Cursor cur = cr.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 (name.equals(selected) && Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {

            Cursor pCur = cr.query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " =?", new String[] {
                id
            }, null);
            finalsend = ContactsContract.CommonDataKinds.Phone.NUMBER;
            while (pCur.moveToNext()) {

            }
            pCur.close();

        }

该代码应该查看联系人以找到与“选定”变量匹配的联系人,该变量似乎工作正常,但随后应该切换到 ContactsContract.CommonDataKinds.Phone.CONTENT_URI 并找到相同的内容通过匹配 ID 来联系我,并向我提供为该联系人保存的电话号码。

每次它返回“Data1”时,我做错了什么?这可能是一个愚蠢的错误,但我们将不胜感激。

ContentResolver cr = getContentResolver();
Cursor cur = cr.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 (name.equals(selected) && Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {

            Cursor pCur = cr.query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " =?", new String[] {
                id
            }, null);
            finalsend = ContactsContract.CommonDataKinds.Phone.NUMBER;
            while (pCur.moveToNext()) {

            }
            pCur.close();

        }

The code is supposed to look through contacts to find one that matches the "selected" variable which appears to work fine but then it is supposed to switch to the ContactsContract.CommonDataKinds.Phone.CONTENT_URI and find the same contact by matching up the ID's and give me the phone number saved for that contact.

Every time it returns "Data1", what am I doing wrong? It's probably a stupid mistake but any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

口干舌燥 2024-12-23 16:32:04

以下代码片段对我有用:

String id , name;
ContentResolver cr = getContentResolver();

     String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

     Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,  null, null, null, sortOrder);



      if (cur.getCount() > 0) {


            while (cur.moveToNext()) {



                id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));

                name = cur.getString( cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                Log.i(tag, "Id is "+ id+"\t Name is"+name);

            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0){                      


                        Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI,

                            null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);



                        while (pCur.moveToNext()) {


                // Do something with phones



                       int phNumber = pCur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);

                       String phn = pCur.getString(phNumber);

                       Log.i("phn number",  phn);



                    } 

                    pCur.close();

                }

                }

    }

Following code snippet is working for me:

String id , name;
ContentResolver cr = getContentResolver();

     String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

     Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,  null, null, null, sortOrder);



      if (cur.getCount() > 0) {


            while (cur.moveToNext()) {



                id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));

                name = cur.getString( cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                Log.i(tag, "Id is "+ id+"\t Name is"+name);

            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0){                      


                        Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI,

                            null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);



                        while (pCur.moveToNext()) {


                // Do something with phones



                       int phNumber = pCur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);

                       String phn = pCur.getString(phNumber);

                       Log.i("phn number",  phn);



                    } 

                    pCur.close();

                }

                }

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文