我在 Android 中使用 ContactPicker 时遇到问题。我的代码是下一个
public void showContacts(View v){
phoneNumber.setText("");
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, 1001);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case 1001:
Cursor cursor = null;
String[] numeros = null;
try {
Uri result = data.getData();
String id = result.getLastPathSegment();
cursor = getContentResolver().query(Phone.CONTENT_URI,
null,
Phone.CONTACT_ID + "=?", new String[] {id}, null);
int phoneIdx = cursor.getColumnIndex(Phone.NUMBER);
if(cursor.getCount() > 0){
numeros = new String[cursor.getCount()];
if (cursor.moveToFirst()) {
int cont = 0;
do{
numeros[cont] = cursor.getString(phoneIdx);
cont++;
}while(cursor.moveToNext());
}
}
} catch (Exception e) {
Log.e("Exception", "Failed to get email data", e);
} finally {
if (cursor != null) {
cursor.close();
}
if(numeros != null){
if(numeros.length > 1){
showNumbers(numeros);
}else{
phoneNumber.setText(numeros[0]);
}
}
}
break;
}
}
}
private void showNumbers(final String[] numeros){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setSingleChoiceItems(numeros, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogo, int item) {
phoneNumber.setText(numeros[item]);
dialogo.cancel();
}
});
builder.create().show();
}
基本上在第一种方法中我调用 ContactPicker,在第二种方法中我收到这个答案,我处理它并验证用户是否有多个电话。
问题是,算法返回我六倍的数字。我不知道为什么会出现这种情况。