我正在尝试允许用户在Android中使用联系人选择器插入他们的号码。我目前正在使用Getting Number from Contacts Picker中的帖子2中的示例出现联系人选择器,但是当我选择联系人时,联系人号码不会影响我的edittext内部。 没有logcat错误或者无论如何。
我的代码:
public void doLaunchContactPicker(View view) {
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case CONTACT_PICKER_RESULT:
Cursor cursor = null;
String phoneNumber = "";
List<String> allNumbers = new ArrayList<String>();
int phoneIdx = 0;
try {
Uri result = data.getData();
String id = result.getLastPathSegment();
cursor = getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + "=?", new String[] { id }, null);
phoneIdx = cursor.getColumnIndex(Phone.DATA);
if (cursor.moveToFirst()) {
while (cursor.isAfterLast() == false) {
phoneNumber = cursor.getString(phoneIdx);
allNumbers.add(phoneNumber);
cursor.moveToNext();
}
} else {
//no results actions
}
} catch (Exception e) {
//error actions
} finally {
if (cursor != null) {
cursor.close();
}
final EditText phoneInput = (EditText) findViewById(R.id.mobileno);
final CharSequence[] items = allNumbers.toArray(new String[allNumbers.size()]);
AlertDialog.Builder builder = new AlertDialog.Builder(SIMMessageSenderActivity.this);
builder.setTitle("Choose a number");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
String selectedNumber = items[item].toString();
selectedNumber = selectedNumber.replace("-", "");
phoneInput.setText(selectedNumber);
}
});
AlertDialog alert = builder.create();
if(allNumbers.size() > 1) {
alert.show();
} else {
String selectedNumber = phoneNumber.toString();
selectedNumber = selectedNumber.replace("-", "");
phoneInput.setText(selectedNumber);
}
if (phoneNumber.length() == 0) {
//no numbers found actions
}
}
break;
}
} else {
//activity result error actions
}
}
答案 0 :(得分:1)
以下代码是我正在使用的代码,它对我来说非常好。试试这个吧。
if((requestCode == PICK_CONTACT) && (resultCode == RESULT_OK))
{
if (data != null) {
Uri contactData = data.getData();
try {
String id = contactData.getLastPathSegment();
String[] columns = {Phone.DATA,Phone.DISPLAY_NAME};
Cursor phoneCur = getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
columns ,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id },
null);
final ArrayList<String> phonesList = new ArrayList<String>();
String Name = null ;
if(phoneCur.moveToFirst())
{
do{
Name = phoneCur.getString(phoneCur.getColumnIndex(Phone.DISPLAY_NAME));
String phone = phoneCur
.getString(phoneCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
phonesList.add(phone);
} while (phoneCur.moveToNext());
}
phoneCur.close();
if (phonesList.size() == 0) {
Toast.makeText(
this,"This contact does not contacin any number",
Toast.LENGTH_LONG).show();
} else if (phonesList.size() == 1) {
toET.setText(phonesList.get(0));
} else {
final String[] phonesArr = new String[phonesList
.size()];
for (int i = 0; i < phonesList.size(); i++) {
phonesArr[i] = phonesList.get(i);
}
AlertDialog.Builder dialog = new AlertDialog.Builder(
MessageManagerActivity.this);
dialog.setTitle("Name : "+Name);
((Builder) dialog).setItems(phonesArr,
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
String selectedEmail = phonesArr[which];
toET.setText(selectedEmail);
}
}).create();
dialog.show();
}
} catch (Exception e) {
Log.e("FILES", "Failed to get phone data", e);
}
}
}
将您的edittext创建为类变量..所以您也可以应用空检查,我非常确定您是为此目的再次创建它..