0

I am trying to show contacts on invite activity using recycler view but could not manage to show I have taken contact permission on recent page and here is my code.

in model there are only 2 parameters so i think it is not important to put that in here thank you in advance

InviteActivity.java

    recyclerViewInviteUser.setHasFixedSize(true);
    recyclerViewInviteUser.setLayoutManager(new LinearLayoutManager(InviteActivity.this));

    loadContacts();

   


private void loadContacts() {
    Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null,
            null,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC");

    ArrayList<ContactModel> arrayListContact = new ArrayList<>();

    if (cursor.getCount() > 0){
        while (cursor.moveToNext()){
            @SuppressLint("Range") String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            @SuppressLint("Range") String number = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

            if (number.length() > 0){
                Cursor phoneCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"=?", new String[]{id}, null);

                if (phoneCursor.getCount() > 0){
                    while (phoneCursor.moveToNext()){
                        @SuppressLint("Range") String phoneNumberValue = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                        ContactModel contactModel = new ContactModel(name,phoneNumberValue);

                        arrayListContact.add(contactModel);
                    }
                } phoneCursor.close();
            }
        }

       
        ContactAdapter contactAdapter = new ContactAdapter(InviteActivity.this, arrayListContact);
        recyclerViewInviteUser.setAdapter(contactAdapter);
        contactAdapter.notifyDataSetChanged();

    } cursor.close();
}
2
  • 1
    Questions like "please debug my code" don't fit well with StackOverflow, you need to find a very specific issue and ask about that, not post your code and ask for help. Anyway browsing through your code i've found a number of issues: "HAS_PHONE_NUMBER" is an integer not a string, and then the "if" following it should be number == 1. you're adding a new contact for each phone number you find. you can run a single query for all the data instead of one per contact (which is very slow). You should add a lot of logs within your code to check which part exactly is not working as expected
    – marmor
    Dec 30, 2021 at 7:24
  • here's a link to a similar question with an answer that does this with a single query instead of multiple ones: stackoverflow.com/a/67088436/819355
    – marmor
    Dec 30, 2021 at 7:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.