0

my code below is working fine...but it gives me only one contact number upon selection whereas i want to select different / multiple contact numbers and selected contacts to be shown on the screen...

have tried loops and all but not getting where exactly i am doing wrong so as not to achieve what i require....

can someone help me with this ??

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();
    }
}

}

  • Here check this [answer](https://stackoverflow.com/a/23577815/5588347). – StackUseR Sep 18 '17 at 09:29
  • @AshishSrivastava i have tried this as well...and its working too but it gives me random contacts starting from T whereas it should start from A as like my phone book....if u could help me with that answer..it would be good too – Zainab Zafar Sep 18 '17 at 10:09

0 Answers0