获取和显示联系人

发布于 2024-10-06 16:39:18 字数 1244 浏览 2 评论 0原文

好吧,我一直在四处寻找,但找不到实际显示联系人列表的具体方法。我找到了诸如 high pass 之类的教程,但它从来没有实际上讨论了如何显示它们,只是如何获取它们。我只是想在 listView 或类似的东西中显示联系人列表。我知道它一定比我想象的要简单得多,因为这似乎是一件常见的事情。具体来说,我想要的只是联系人的姓名和电话号码。我已经设置了查询,我得到了上面提到的教程,我认为它是正确的:

        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 (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()) {
                } 
                pCur.close();
        }
        }
}

Well, I've been looking around and can't find a specific way to actually DISPLAY the contacts list. I've found tutorials such as higher pass's but it never actually discusses how to display them, just how to get them. I simply want to display the contacts list in a listView or something similar. I know it has to be a lot more simple than I'm making it out to be, because it seems to be a common thing. Specifically, all I want is the contact's name and phone numbers. I have my query set up, which I got the above mentioned tutorial, and I think it's right:

        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 (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()) {
                } 
                pCur.close();
        }
        }
}

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

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

发布评论

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

评论(1

影子是时光的心 2024-10-13 16:39:18

为此有一个内置活动:

startActivity(new Intent(Intent.ACTION_VIEW, ContactsContract.Contacts.CONTENT_URI));

如果您使用 startActivityForResult(),您甚至可以取回用户选择的联系人。

如果这不是您想要的,您希望在可能的情况下减少到单个查询,然后为您的 ListView 创建一个 Adapter这是一个示例项目,它显示了查询联系人以填充ListView,显示联系人姓名、姓名和电话号码,或者姓名和电子邮件地址。这变得有点复杂,因为示例显示了 Android 1.6 及更低版本和 Android 2.0 及更高版本的联系人 API。

There is a built-in activity for this:

startActivity(new Intent(Intent.ACTION_VIEW, ContactsContract.Contacts.CONTENT_URI));

If you use startActivityForResult(), you can even get the user's chosen contact back.

If that's not what you're looking for, you want to cut back to a single query if possible, then create an Adapter for your ListView. Here is a sample project that shows querying the contacts to populate a ListView, showing either the contact's name, name and phone number, or name and email address. This gets a bit more complicated because the sample shows both the Android 1.6-and-lower and Android 2.0-and-higher contacts APIs.

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