How to display Contacts in alphabetic order in Listview [closed]
Asked Answered
T

4

13

I have a custom List view and i need to display the contacts in Alphabetical order can u provide the sample code how can i achieve this?

Tamer answered 6/9, 2011 at 8:8 Comment(0)
F
41

you can get contacts in alphabatical order like this:

Cursor cursor = getContentResolver.query(Phone.CONTENT_URI, null, null, null, Phone.DISPLAY_NAME + " ASC");

refer these links for more cearity:

How to call Android contacts list?

How to get contacts from native phonebook in android

How to obtain all details of a contact in Android

How to get the first name and last name from Android contacts?

How to import contacts from phonebook to our application

Android contacts extraction

How to get all android contacts but without those which are on SIM

For list you can refer this tutorial:

http://www.vogella.de/articles/AndroidListView/article.html

Furze answered 6/9, 2011 at 8:13 Comment(0)
U
2

You need to do this:

Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
Underact answered 14/9, 2017 at 11:36 Comment(1)
Thanks. I wonder why the question owner didn't accept itFreshwater
A
0

use this code to retrieve the contacts

ArrayList<HashMap<String,String>> contactData=new ArrayList<HashMap<String,String>>();
ContentResolver cr = getContentResolver();
     Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
     while (cursor.moveToNext()) {
         try{
         String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
         String name=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
         String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
         if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
             Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
             while (phones.moveToNext()) { 
                 String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
                 HashMap<String,String> map=new HashMap<String,String>();
                 map.put("name", name);
                 map.put("number", phoneNumber);
                 contactData.add(map);
             } 
             phones.close(); 
         }
     }catch(Exception e){}
     }

Now the contact details are stored in the contactData ArrayList. Now sort this arrayList like bellow

Collections.sort(contactData, new Comparator(){
                @Override
                public int compare(Object o1, Object o2) {
                    HashMap map1=(HashMap)o1;
                    HashMap map2=(HashMap)o2;
                    String s1=(String)map1.get("name");
                    String s2=(String)map2.get("name");
                    return s1.compareTo(s2);
                }
            });

Now the contact details are stored in the sorting order in the contactData ArrayList.

Argosy answered 6/9, 2011 at 9:2 Comment(1)
This worked , but there are duplicates in each contactLeverhulme
G
0

I have added Alphabetic headers to contact list

new ReadContactTask(context, showProgressDialog,new OnContactReadListener() {
            @Override
            public void onContactRead(List<ContactItem> dummyList) {

                ArrayList<ContactItem> latestPhoneContacts = new ArrayList<>();

                if(dummyList!=null && dummyList.size()>0)
                {
                    tvNoResult.setVisibility(View.GONE);

                    for(ContactItem contactItem : dummyList){
                        String contactNameInCapital = "";
                        if(contactItem.getContactName()!=null && contactItem.getContactName().length()>2){
                            contactNameInCapital = contactItem.getContactName().substring(0,1).toUpperCase();

                            if(contactNameInCapital.charAt(0) != previousContact.charAt(0))
                            {
                                latestPhoneContacts.add(new ContactItem(contactNameInCapital.substring(0,1)));
                                latestPhoneContacts.add(contactItem);
                            }
                            else
                            {
                                latestPhoneContacts.add(contactItem);
                            }

                            previousContact = contactNameInCapital;
                        }
                    }
                    if(latestPhoneContacts!=null && latestPhoneContacts.size()>0)
                        contactListAdapter.addItems(latestPhoneContacts);
                }
            }

            @Override
            public void onContactEmpty() {

            }
        }).execute();

where

private String previousContact ="Zee";
Getraer answered 22/6, 2016 at 9:48 Comment(1)
what is ContactItemLeverhulme

© 2022 - 2024 — McMap. All rights reserved.