Android从联系人检索数据

时间:2013-08-17 18:21:35

标签: android telephony

我尝试编写应用程序以读取联系人的姓名,姓氏和ID并在listview中显示,我使用以下代码

        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());

但是大日期的这段代码给我带来了错误,我如何使用更轻的代码来获取数据?