javaandroidandroid-studioandroid-min-sdkcontact-list

how to show list contact phone in android studio with sdk 27


i have code java android studio to show list contact in hp and it's permission done, but it's not working (blank value) in sdk >27 anyone help me? this is my code

    contactModelArrayList = new ArrayList<>();
    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            null,
            ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'",
            null,
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" COLLATE LOCALIZED ASC");
    while (phones.moveToNext())
    {
        String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        ContactModel contactModel = new ContactModel();
        contactModel.setName(name);
        contactModel.setNumber(phoneNumber);

        contactModelArrayList.add(contactModel);


        Log.d("name>>",name+" "+phoneNumber);
    }
    Log.d("phones", phones.toString());
    phones.close();

    contactAllAdapter = new ContactAllAdapter(this,contactModelArrayList);
    listView.setAdapter(contactAllAdapter);

in logcat thereis nothing


Solution

  • You can try LoaderManager

    Create following fields

    private static final int CONTACTS_LOADER_ID = 101;
    
    private static final String[] PROJECTION = {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME_PRIMARY,
            ContactsContract.CommonDataKinds.Phone.NUMBER
    };
    

    Create follwoing class

    public class Contact {
    
        private String id;
        private String name;
        private String number;
    
        public Contact(String id, String name, String number) {
            this.id = id;
            this.name = name;
            this.number = number;
        }
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getNumber() {
            return number;
        }
    
        public void setNumber(String number) {
            this.number = number;
        }
    
    }
    

    [1] Create instance of LoaderManager.LoaderCallback

    private LoaderManager.LoaderCallbacks<Cursor> loaderCallbacks = new LoaderManager.LoaderCallbacks<Cursor>() {
            @Override
            public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    
                switch (id) {
                    case CONTACTS_LOADER_ID:
    
                        return new CursorLoader(
                                getActivity(),
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                PROJECTION,
                                null,
                                null,
                                ContactsContract.Contacts.DISPLAY_NAME_PRIMARY + " ASC"
                        );
                    default:
                        return null;
                }
            }
    
            @Override
            public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
                switch (loader.getId()) {
                    case CONTACTS_LOADER_ID:
    
                       //Use ContactUtils class here
    
    
                        break;
                }
            }
    
            @Override
            public void onLoaderReset(Loader<Cursor> loader) {
    
            }
        };
    

    [2] Use following class to convert cursor data to List<Contact>

    import android.content.ContentResolver;
    import android.database.Cursor;
    import android.provider.ContactsContract;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class ContactUtils {
    
        private ContactUtils() {
        }
    
        public static List<Contact> parseContacts(Cursor cursor) {
            List<Contact> contacts = new ArrayList<>();
    
            if (cursor.getCount() == 0) {
                return contacts;
            }
    
            int nameColumnIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY);
            int idColumnIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID);
            int numberColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
    
            while (cursor.moveToNext()) {
                String contactName = cursor.getString(nameColumnIndex);
                String id = cursor.getString(idColumnIndex);
                String number = cursor.getString(numberColumnIndex);
    
                Contact contact = new Contact(id, contactName, number);
                if(!isContactAddedInList(contact, contacts)){
                    contacts.add(contact);
                }
    
            }
    
            return contacts;
        }
    
    public boolean isContactAddedInList(Contact contact, List<Contact> contacts) {
    
        for (Contact listContact : contacts
                ) {
            if (listContact.number.equals(contact.name)) {
                return true;
            }
        }
    
        return false;
    }
    
    
    }
    

    [3] Call getLoaderManager().initLoader method to load contacts in your Fragment or Activity's onStart method

    getLoaderManager().initLoader(CONTACTS_LOADER_ID, null, loaderCallbacks);