0

i try to write application to read name, last name , and id of contact and show in listview, i use following code

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

but this code in large date gives error to me, how can i use lighter code to get data???

Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63
  • possible duplicate of [how to get android contact list data on my seperate listview in android 2.1?](http://stackoverflow.com/questions/7803633/how-to-get-android-contact-list-data-on-my-seperate-listview-in-android-2-1) – Deepak Swami Aug 17 '13 at 18:29
  • i can get data, but i need code that either in large data working fine – Shayan Pourvatan Aug 17 '13 at 18:46
  • This link may help : [http://stackoverflow.com/questions/4330565/using-cursor-with-listview-adapter-for-a-large-amount-of-data][1] [1]: http://stackoverflow.com/questions/4330565/using-cursor-with-listview-adapter-for-a-large-amount-of-data – S.Thiongane Aug 17 '13 at 19:21

1 Answers1

0

have a look on these links may help you

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?

Community
  • 1
  • 1
Deepak Swami
  • 3,838
  • 1
  • 31
  • 46
  • but my code is true, and when i run into emulator is worked fine, but in real device and large data (260 contact) is going to crash – Shayan Pourvatan Aug 17 '13 at 18:39