1

I am trying to display phone contacts in a listview but UI not updating after taking permission on first time.I can only see changes when I restart App. I have tried to use notifyDataSetChanged but nothing changed.

 public class ContactFragment extends Fragment {
         ArrayList<Contact> contact;
         ListView listView;
         ContactAdapter adapter;
         public static final int RequestPermissionCode = 1;


    Cursor cursor;
    String name, number;

    public ContactFragment() {

    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.contact_layout, container, false);
contact=new ArrayList<>();
        accesscontact();


        adapter = new ContactAdapter(getActivity(), contact);

         listView = (ListView) rootView.findViewById(R.id.listview);



        listView.setAdapter(adapter);
        return rootView;
    }
    public void GetContactsIntoArrayList(){

        cursor = getActivity().getApplicationContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null, null, null);

        while (cursor.moveToNext()) {

            name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

            number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            contact.add(new Contact(name,number));
        }
        cursor.close();

    }
    protected void accesscontact()
    {
    if (ContextCompat.checkSelfPermission(getActivity(),
    Manifest.permission.READ_CONTACTS)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(getActivity(),
                new String[]{Manifest.permission.READ_CONTACTS},
                RequestPermissionCode);
    } else {

        GetContactsIntoArrayList();


    }
}


    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case RequestPermissionCode: {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    GetContactsIntoArrayList();



                } else {
                    Toast.makeText(getActivity().getApplicationContext(),
                            "Can't read contact_layout without permission", Toast.LENGTH_LONG).show();
                    return;
                }
            }
        }
    }
priyanshu goyal
  • 289
  • 1
  • 2
  • 8
  • (1) add a log to your `GetContactsIntoArrayList` and see that it's called as expected after getting the permission. (2) add another log when it's done with the number of contacts you were able to find. (3) follow @neelsnallu answer, and re-create or refresh your adapter after updating the `contact` ArrayList – marmor May 22 '17 at 09:55

1 Answers1

1

Try the following:

 adapter = new ContactAdapter(getActivity(), contact);
 listView = (ListView) rootView.findViewById(R.id.listview);
 listView.setAdapter(adapter);

use these lines into GetContactsIntoArrayList() method

public void GetContactsIntoArrayList(){

        cursor = getActivity().getApplicationContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null, null, null);

        while (cursor.moveToNext()) {

            name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

            number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            contact.add(new Contact(name,number));
        }
        cursor.close();
 adapter = new ContactAdapter(getActivity(), contact);
 listView = (ListView) rootView.findViewById(R.id.listview);
 listView.setAdapter(adapter);

    }

Hope this will work.

neelsnallu
  • 101
  • 7