从单个联系人处获取所有电话号码并在edittext中设置

时间:2013-10-19 17:42:45

标签: android

我想在edittext中获取所选联系人的所有电话号码。我的问题是edittext只显示所选联系人的第一个电话号码。假设联系人有两个数字,如12345和00001.我的edittext只显示第一个数字12345,但我想要12345,00001。

我正在尝试这个:

    Cursor cursor = null; 
    String phoneNumber = "";
    int phoneIdx = 0;
    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()) {
        phoneNumber = cursor.getString(phoneIdx);
        cursor.moveToNext();
    }
    else {

    }  
} 
finally {  
    if (cursor != null) {  
        cursor.close();
    }
    final EditText phoneInput = (EditText) findViewById(R.id.contact_phone);
    nameEntry.setText(phoneNumber);
}  

1 个答案:

答案 0 :(得分:0)

    cursor.moveToFirst();
    while (cursor.isAfterLast() != true) {
        if (phoneNumber.equals("")) {
            phoneNumber = cursor.getString(phoneIdx);
        }

        else {
            phoneNumber = phoneNumber + "," + cursor.getString(phoneIdx);   
        }
        cursor.moveToNext();

     }

而不是

if (cursor.moveToFirst()) {
                phoneNumber = cursor.getString(phoneIdx);
                    cursor.moveToNext();


            }