从Android ContactsContract数据库加载数据后,片段没有填充

时间:2017-05-18 19:06:56

标签: android listview android-adapter contactscontract

我正在尝试在列表视图中显示手机联系人,但在第一次获得权限后UI未更新。我只能在重新启动App时看到更改。 我试过使用notifyDataSetChanged但没有改变。

 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;
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

尝试以下方法:

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

将这些行用于GetContactsIntoArrayList()方法

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);

    }

希望这会奏效。