联系人选取器未返回结果

qcuzuvrc  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(434)

在输入框中,我抛出一个对话框来获取联系人。我的代码如下所示

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toContacts = (EditText)findViewById(R.id.To);
        toContacts.setOnClickListener(new EditText.OnClickListener(){public void onClick(View v){pickContacts();}});

    }

    public void pickContacts(){
        Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
        startActivityForResult(intent, 1);
    }

    public void onActivityResult(int reqCode, int resultCode, Intent data){
        super.onActivityResult(reqCode, resultCode, data);
        String name = "";
          switch (reqCode) {
            case (1) :
              if (resultCode == Activity.RESULT_OK) {
                Uri contactData = data.getData();
                //@SuppressWarnings("deprecation")
                Cursor c = null;
                try{
                     //c =  managedQuery(contactData, null, null, null, null);
                     c = getContentResolver().query(contactData, null, Contacts.DISPLAY_NAME, null, null);
                     if (c.moveToFirst()) {
                          name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                          //String number = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                        }
                } catch(Exception e){
                    Log.e(DEBUG_TAG, "failed to get contacts data", e);
                } finally {
                    if(c != null){
                        c.close();
                    }
                    EditText toSendContact = (EditText) findViewById(R.id.To);
                    toSendContact.setText(name);
                }
              }
              break;
          }

    }

但它不会将所选结果返回到输入框中。

igetnqfo

igetnqfo1#

代替

c = getContentResolver().query(contactData, null, Contacts.DISPLAY_NAME, null, null);

具有

c = getContentResolver().query(contactData, new String[] { ContactsContract.Contacts.DISPLAY_NAME }, null, null, null);

您使用了选择而不是投影。

相关问题