如何在 android 2.1 中的单独列表视图上获取 android 联系人列表数据?

发布于 2024-12-10 11:57:46 字数 160 浏览 0 评论 0原文

我已经创建了一个列表视图。现在我想在我的列表视图上显示 Android(电话簿)联系人列表中的数据(联系人号码、姓名)。 我得到了 android 1.5,但我想为 android2.1updated 做到这一点。

如何做到这一点?任何人都可以指导我或提供一些示例代码吗? 提前致谢 - -

i have created one listview.now i want to show data(contact no.,name) from contactlist of android(Phonebook) on my listview.
i got it for android 1.5 but i want to do it for android2.1updated.

How to do it?can any one guide me or give some sample code?
Thanks in advance---

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

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

发布评论

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

评论(2

醉梦枕江山 2024-12-17 11:57:46

如果您在 1.5 中实现了这一点,那么 URI 可能会略有不同。您应该在 2.1 中获取更新/更改的联系人 URI。为了进一步理解,这里有一个 2.1updated 的示例代码:

ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cur.moveToNext()) 
        {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));

            //---------------------------------------- Contact Names        
            //------------------------------------------------------
            String DisplayName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            Cursor names = cr.query(ContactsContract.Data.CONTENT_URI, null,ContactsContract.Data.CONTACT_ID + " = " + id, null, null);
            names.moveToNext();
            String GivenName = names.getString(names.getColumnIndex(ContactsContract.Data.DATA2));
            String FamilyName = names.getString(names.getColumnIndex(ContactsContract.Data.DATA3));
            String MiddleName = names.getString(names.getColumnIndex(ContactsContract.Data.DATA5));
            names.close();


            //----------------------------------------- Phone Numbers
            //-------------------------------------------------------
            String hasPhoneNumber = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
            if (hasPhoneNumber.equals("1"))
            {
                Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
                while (phones.moveToNext()) 
                {
                                      ......
                }
            }
        } // AND SO ON.... Get which ever data you need.

所以上面只是我用于我的任务的示例代码。您可以通过进行相应的更改来获得所需的内容。我希望它有帮助。

If you have achieved it in 1.5 then there can be a small difference of URI. You should go for updated/changed URI of Contacts in 2.1. For further understanding here is a sample code for 2.1updated:

ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cur.moveToNext()) 
        {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));

            //---------------------------------------- Contact Names        
            //------------------------------------------------------
            String DisplayName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            Cursor names = cr.query(ContactsContract.Data.CONTENT_URI, null,ContactsContract.Data.CONTACT_ID + " = " + id, null, null);
            names.moveToNext();
            String GivenName = names.getString(names.getColumnIndex(ContactsContract.Data.DATA2));
            String FamilyName = names.getString(names.getColumnIndex(ContactsContract.Data.DATA3));
            String MiddleName = names.getString(names.getColumnIndex(ContactsContract.Data.DATA5));
            names.close();


            //----------------------------------------- Phone Numbers
            //-------------------------------------------------------
            String hasPhoneNumber = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
            if (hasPhoneNumber.equals("1"))
            {
                Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
                while (phones.moveToNext()) 
                {
                                      ......
                }
            }
        } // AND SO ON.... Get which ever data you need.

so above is just a sample code that I used for my task. You can get what you need by making changes accordingly. I hope it helps.

痴情换悲伤 2024-12-17 11:57:46

这将帮助您获得联系人。

Cursor contactList = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null,null, null);

要获取特定联系人的电话号码,您需要使用:

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

Cursor phoneCursor = getContentResolver().query(
                      ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                      new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER}, 
                      ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                      new String[]{id}, null);

This will help you getting contacts.

Cursor contactList = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null,null, null);

For getting phone number of a particular contact,you need to use:

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

Cursor phoneCursor = getContentResolver().query(
                      ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                      new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER}, 
                      ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                      new String[]{id}, null);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文