首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android从联系人检索数据

Android从联系人检索数据
EN

Stack Overflow用户
提问于 2013-08-18 02:21:35
回答 1查看 199关注 0票数 0

我尝试编写应用程序来读取联系人的姓名和id,并在列表视图中显示,我使用以下代码

代码语言:javascript
复制
        Cursor cursor = getContentResolver().query(
                    ContactsContract.Contacts.CONTENT_URI, null, null,
                    null, null);
            SmsMultiCasting.selectedRow = new int[cursor.getCount()];
            cursor.moveToFirst();
            // data = new String[cursor.getCount()][12];
            if (cursor.getCount() > 0) {
                do {

                    try {
                        i++;
                        Log.d("number", String.valueOf(i));
                        // numberPhone = 0;
                        contactId = cursor
                                .getString(cursor
                                        .getColumnIndex(ContactsContract.Contacts._ID));

                        Uri contactUri = ContentUris.withAppendedId(
                                Contacts.CONTENT_URI,
                                Long.parseLong(contactId));
                        Uri dataUri = Uri.withAppendedPath(contactUri,
                                Contacts.Data.CONTENT_DIRECTORY);

                        Cursor phones = getContentResolver()
                                .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                        null,
                                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                                + " = " + contactId, null,
                                        null);
                        int phonenumber = 0;
                        while (phones.moveToNext()) {
                            phonenumber++;
                        }

                        if (phonenumber > 0)
                        // phones.moveToNext();
                        {

                            try {
                                Cursor nameCursor = getContentResolver()
                                        .query(dataUri,
                                                null,
                                                Data.MIMETYPE + "=?",
                                                new String[] { StructuredName.CONTENT_ITEM_TYPE },
                                                null);
                                nameCursor.moveToFirst();
                                do {

                                    String firstName = nameCursor
                                            .getString(nameCursor
                                                    .getColumnIndex(Data.DATA2));

                                    String lastName = "";

                                    String displayname = cursor
                                            .getString(cursor
                                                    .getColumnIndex(Contacts.DISPLAY_NAME_ALTERNATIVE));
                                    if (!firstName.equals(displayname)) {
                                        lastName = nameCursor
                                                .getString(nameCursor
                                                        .getColumnIndex(Data.DATA3));
                                    }

                                    if (firstName.equals(null)
                                            && lastName.equals(null))
                                        SmsMultiCasting.Arrayefullname
                                                .add("unknown name");
                                    else if (firstName.equals(null))
                                        SmsMultiCasting.Arrayefullname
                                                .add(lastName);
                                    else if (lastName.equals(null))
                                        SmsMultiCasting.Arrayefullname
                                                .add(firstName);
                                    else
                                        SmsMultiCasting.Arrayefullname
                                                .add(firstName + " "
                                                        + lastName);
                                        SmsMultiCasting.Arrayeid.add(String
                                            .valueOf(firstName + " "
                                                    + lastName + "//"
                                                    + contactId));

                                } while (nameCursor.moveToNext());
                                nameCursor.close();
                            } catch (Exception e) {
                            }
                        }
                    }

                    catch (Exception t) {
                    }

                      String image_uri = cursor
                                .getString(cursor
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
                      SmsMultiCasting.ArrayePhoto.add(image_uri);
                */    

                } while (cursor.moveToNext());

但是这个大数据中的代码给了我错误,我如何使用轻量级的代码来获取数据?

EN

回答 1

Stack Overflow用户

发布于 2013-08-18 02:29:11

看看这些链接可能会对你有所帮助。

http://developer.android.com/training/contacts-provider/retrieve-names.html

How to show phone contacts in a ListView

how to get android contact list data on my seperate listview in android 2.1?

show List of all contacts with phonenumber in android?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18291984

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档