从安卓联系人合同数据库加载数据后片段未填充



我正在尝试在列表视图中显示电话联系人,但第一次获得权限后UI未更新。我只能在重新启动应用程序时看到更改。我尝试使用通知数据集更改,但没有任何更改。

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

尝试以下操作:

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

希望这会起作用。

相关内容

  • 没有找到相关文章

最新更新