在输入框中,我正在抛出一个对话框来接收联系人。我的代码如下所示
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;
}
}
但它没有将所选结果返回到输入框中。
答案 0 :(得分:0)
替换
c = getContentResolver().query(contactData, null, Contacts.DISPLAY_NAME, null, null);
与
c = getContentResolver().query(contactData, new String[] { ContactsContract.Contacts.DISPLAY_NAME }, null, null, null);
您已使用选择投影。