0

I have an application where I want to fetch multiple numbers of a single contact on the click of the item. I have a list view of contact in which I want multiple numbers of a contact if the contact has more than 1 number. Here is my code , I get all the numbers but can't get the multiple numbers on Click.

MainActivity.java

 ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
            null, null, null);
    String phone = null;
    String emailContact = null;
    String emailType = null;
    String image_uri = "";
    Bitmap bitmap = 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));

            image_uri = cur
                    .getString(cur
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
            if (Integer
                    .parseInt(cur.getString(cur
                            .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                System.out.println("name : " + name + ", ID : " + id);

                Cursor pCur = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = ?", new String[]{id}, null);
                while (pCur.moveToNext())
                {
                    phone = pCur
                            .getString(pCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                  String  phone1 = pCur
                            .getString(pCur
                                    .getColumnIndex(String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)));
                        Log.e("PHONE",phone1);
                    System.out.println("phone" + phone);
                }
                pCur.close();

                Cursor emailCur = cr.query
                        (
                        ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Email.CONTACT_ID
                                + " = ?", new String[]{id}, null);
                while (emailCur.moveToNext())
                {
                    emailContact = emailCur
                            .getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));


if(TextUtils.isEmpty(emailContact)||emailContact.equalsIgnoreCase(null)||
emailContact.equalsIgnoreCase("")){
                        emailContact="";

                        Log.e("isEmpty","isEmpty " + emailContact);
                    }else{
                        Log.e("gfdszfg","Email " + emailContact);
                    }
                  /*  emailType = emailCur
                            .getString(emailCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));*/

                    Log.e("gfdszfg","Email " + emailContact);

                }

                emailCur.close();
            }

            if (image_uri != null) {
                System.out.println(Uri.parse(image_uri));
                try {
                    bitmap = MediaStore.Images.Media
                            .getBitmap(this.getContentResolver(),
                                    Uri.parse(image_uri));

                    System.out.println(bitmap);

                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            mList.add(new Contacts(name, phone, image_uri,emailContact));
            emailContact="";
        }
        cur.close();
        mMyContactsAdapter = new MyContactsAdapter(MainActivity.this,
   mList);
        mcontact.setAdapter(mMyContactsAdapter);
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

0

It seems like you're properly iterating through all phones for each contact, but when the while loop ends, phone is pointing only to the last phone you've found.

...
List<String> phonesList = new ArrayList<String>();
while (pCur.moveToNext()) {
    phone = pCur.getString( ... Phone.NUMBER));
    // add the phone to a growing list of phones
    phonesList.add(phone);
}
pCur.close();
...
// Change 'Contacts' constructor to accept List<String>, rather then String for phone
mList.add(new Contacts(name, phonesList, image_uri,emailContact));
...

Of course you can do the same for emails, and if you want the label of the phone too, you'll need a list of pairs: List<Pair<String, String>> to hold pairs of phone+label.

On a side note the performance of this code can be improved significantly by querying ContactsContract.Data.CONTENT_URI for ALL emails and phones on the device, putting them all in a HashMap, and then iterating all contacts, and getting the emails and phones from the HashMap. This will reduce the number of queries from <number of contacts>*2 to just 2.

marmor
  • 27,641
  • 11
  • 107
  • 150
  • how will i get multiple numbers of a contact when i will click on my listview @marmor – Prashant Jaiswal Feb 03 '17 at 09:20
  • if you setup the initial contacts list as I suggested above, you'll have a list of 'Contacts' objects, each containing a list of phones. so when an item is clicked, you don't need to query for anything from the DB, just get the list of phones from the clicked 'Contacts' object – marmor Feb 03 '17 at 17:15