我从手机中获取联系人到对话框中显示时出错

时间:2018-03-29 08:57:33

标签: android listview android-fragments alertdialog android-contacts

  btn_add_from_contacts.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            builder = new AlertDialog.Builder(getActivity());
            View view = getLayoutInflater().inflate(R.layout.layout_add_from_contacts, null);
            builder.setView(view);
            /* ********************* */
            phones = getActivity().getApplication().getContentResolver().query
            (ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");

            String name,phoneNumber;

            while(phones.moveToNext()) {
                name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                Log.w("","Names: " +name+"\nPhone: "+phoneNumber);
                alertDialog = builder.create();
                alertDialog.show();
            }

            /* ********************* */




        }
    });

这是代码,我想要的就是在这里显示来自手机的联系人。我只是在while循环中显示它,但它没有给我任何输出。我还在使用片段。如果有人知道代码有什么问题,请帮助我。

这是Logcat:

03-29 09:14:29.475 16754-16759/com.example.nomanikram.epilepsyseizuredetection I/zygote: Increasing code cache capacity to 1024KB

03-29 09:14:31.342 16754-16754 / com.example.nomanikram.epilepsyseizuredetection W / InputEventReceiver:尝试完成输入事件但输入事件接收器已被处理掉。 03-29 09:14:31.343 16754-16754 / com.example.nomanikram.epilepsyseizuredetection W / ViewRootImpl [MainActivity]:由于删除了根视图而导致的丢弃事件:MotionEvent {action = ACTION_UP,actionButton = 0,id [0] = 0,x [0] = 116.09668,y [0] = 382.5547,toolType [0] = TOOL_TYPE_FINGER,buttonState = 0,metaState = 0,flags = 0x0,edgeFlags = 0x0,pointerCount = 1,historySize = 0,eventTime = 10351594 ,downTime = 10351588,deviceId = 0,source = 0x1002} 03-29 09:14:31.343 16754-16754 / com.example.nomanikram.epilepsyseizuredetection W / InputEventReceiver:尝试完成输入事件但输入事件接收器已经被处理掉。

2 个答案:

答案 0 :(得分:0)

将所有联系人添加到

的可能性
R.layout.layout_add_from_contacts

(如果它是垂直线性布局会很有帮助)。你可以通过膨胀和填充单独的视图来做到这一点,比如

R.layout.contact_item, 

(您需要为每个联系人创建)并将其添加到线性布局中,该布局将用作对话窗口的视图。

其他更简单的解决方案是使用ArrayAdapter,如下所示: How can I display a list view in an Android Alert Dialog?

答案 1 :(得分:0)

您可以执行此操作以显示联系人

        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Contacts List");
        ArrayList<String> array=new ArrayList<String>();
        phones = getActivity().getApplication().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
        null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");

        String name,phoneNumber;

        while(phones.moveToNext()) {
            name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            Log.w("","Names: " +name+"\nPhone: "+phoneNumber);
            array.add(name+"-->"+phoneNumber);
        }
        builder.setItems(array, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int position) {
                        //item clicked   
                        }
                    });
        builder.show();