选择不同/多个联系号码

时间:2017-09-18 09:24:29

标签: android listview contact

我的下面的代码工作正常...但它在选择时只给我一个联系号码,而我想选择不同的/多个联系号码和选定的联系人在屏幕上显示......

尝试过循环,但是没有得到我错误的地方,以免达到我的要求....

有人可以帮我解决这个问题吗?

public class Main2Activity extends Activity {
private static final int RESULT_PICK_CONTACT = 85500;
private TextView textView1;
private TextView textView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    textView1 = findViewById(R.id.textView1);
    textView2 = findViewById(R.id.textView2);
}
public void selectContact(View v)
{

    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
    startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK) {

        switch (requestCode) {
            case RESULT_PICK_CONTACT:
                contactPicked(data);
                break;
        }
    } else {
        Log.e("Main2Activity", "Failed to pick contact");
    }
}

private void contactPicked(Intent data) {
    Cursor cursor = null;
    try {
        String phoneNo = null ;
        String name = null;

                    Uri uri = data.getData();
                    cursor = getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
                    int  phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

        int  nameIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        phoneNo = cursor.getString(phoneIndex);
        name = cursor.getString(nameIndex);

        textView1.setText(name);
        textView2.setText(phoneNo);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

0 个答案:

没有答案