beginner beginner -4 years ago 71
Android Question

get all contacts and get all type of number of single contact name

get all types of a(single)contact


i want to get all contacts with its all types.

example contact name:ABC Mobile:1234567890 ,phone:65465198899,Home 546498948989
so above example would be save as to list as String name=ABC,and all type of numbers to be saved to this list
output: contact name:ABC & Mobile:1234567890 ,phone:65465198899,Home 546498948989;'



problem:


single name,number gets saved twice
code i have tried


public void loadContact() {
//swipeRefreshLayout.setRefreshing(true);
RealmList<ContactClass> list = new RealmList<>();

ContentResolver cr = getActivity().getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
RealmList<ContactClass> numberlist = new RealmList<>();
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

if (cur.getInt(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()) {
ContactClass contactClass = new ContactClass();
contactClass.setId(id);
contactClass.setName(name);

String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int type = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
Log.e("called", phoneNo + " " + name + " " + id);
switch (type) {

case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
contactClass.setNumber(phoneNo);
numberlist.add(contactClass);
Log.e("type", type + "home " + phoneNo + " " + name);

break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
contactClass.setNumber(phoneNo);
numberlist.add(contactClass);
Log.e("type", type + "mobile " + phoneNo + " " + name);

break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
contactClass.setNumber(phoneNo);
numberlist.add(contactClass);
Log.e("type", type + "work " + phoneNo + " " + name);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME:
contactClass.setNumber(phoneNo);
numberlist.add(contactClass);
Log.e("type", type + "faxHome " + phoneNo + " " + name);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK:
contactClass.setNumber(phoneNo);
numberlist.add(contactClass);
Log.e("type", type + "faxWork " + phoneNo + " " + name);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MAIN:
contactClass.setNumber(phoneNo);
numberlist.add(contactClass);
Log.e("type", type + "main " + phoneNo + " " + name);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER:
contactClass.setNumber(phoneNo);
numberlist.add(contactClass);
Log.e("type", type + "other " + phoneNo + " " + name);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM:
contactClass.setNumber(phoneNo);
numberlist.add(contactClass);
Log.e("type", type + "custom " + phoneNo + " " + name);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_PAGER:
contactClass.setNumber(phoneNo);
numberlist.add(contactClass);
Log.e("type", type + "pager " + phoneNo + " " + name);
break;
}
contactClass.setNumberlist(numberlist);
list.add(contactClass);

for (int i = 0; i < numberlist.size(); i++) {
Log.e("numbersize", numberlist.get(i).getNumber() + numberlist.get(i).getName() + numberlist.size() + "");
}


}
pCur.close();
}//1637
}
// saveToRealm(list);
try {
HashSet hs = new HashSet();

hs.addAll(list); // demoArrayList= name of arrayList from which u want to remove duplicates
List<ContactClass> classList = new ArrayList<>();
list.clear();
classList.addAll(hs);
saveToRealm(classList);
Log.e("resize", classList.size() + "");
} catch (Exception e) {
Log.d("savetorealm", e.toString());
}

}


}

Answer Source

If a contact has 2 or more phone numbers, you'll be creating multiple ContactClass for the same contact.

Try changing your main loop to this:

// this loop iterates once per contact
while (cur.moveToNext()) {
    if (cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
        RealmList<ContactClass> numberlist = new RealmList<>();
        String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
        String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

        // create a new ContactClass just once per contact
        ContactClass contactClass = new ContactClass();
        contactClass.setId(id);
        contactClass.setName(name);

        Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                new String[]{id}, null);
        // this loop iterates once per phone for each contact
        while (pCur.moveToNext()) {

            String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            int type = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
            Log.e("called", phoneNo + "  " + name + "  " + id);
            switch (type) {
                ...
            }
        }
        pCur.close();

        // add the new ContactClass just once per contact to list
        contactClass.setNumberlist(numberlist);
        list.add(contactClass);

        for (int i = 0; i < numberlist.size(); i++) {
            Log.e("numbersize", numberlist.get(i).getNumber() + numberlist.get(i).getName() + numberlist.size() + "");
        }

    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download