Multiple EditText fields with add contact buttons.

I am trying to get a contact from the phone book and put the number in the editText. I want to do this action 4 times in the same activity. So each editText with its own add button. I have created an onActivityresult to do this but it doesn't work. Could anyone please help me with this code?

Intent contactIntent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
@Override
protected void onActivityResult(int reqCode, int resultCode, Intent intent) 
{
    super.onActivityResult(reqCode, resultCode, intent);
    if (resultCode == Activity.RESULT_OK) 
    {
         Uri contactData1 = intent.getData();
         Cursor c1 = getContentResolver().query(contactData1, null, null, null, null);
         c1.moveToFirst();
         String cNumber1 = c1.getString(c1.getColumnIndex("data1"));
         TextView tv1;
         tv1 = (TextView)findViewById(R.id.number1);
         tv1.setText(cNumber1);
    }
    if (resultCode == Activity.RESULT_OK) 
    {
         Uri contactData2 = intent.getData();
         Cursor c2 = getContentResolver().query(contactData2, null, null, null, null);
         c2.moveToFirst();
         String cNumber2 = c2.getString(c2.getColumnIndex("data1"));
         TextView tv2;
         tv2 = (TextView)findViewById(R.id.number2);
         tv2.setText(cNumber2);
    }
    if (resultCode == Activity.RESULT_OK) 
    {
         Uri contactData3 = intent.getData();
         Cursor c3 = getContentResolver().query(contactData3, null, null, null, null);
         c3.moveToFirst();
         String cNumber3 = c3.getString(c3.getColumnIndex("data1"));
         TextView tv3;
         tv3 = (TextView)findViewById(R.id.number3);
         tv3.setText(cNumber3);
    }
    if (resultCode == Activity.RESULT_OK) 
    {
         Uri contactData4 = intent.getData();
         Cursor c4 = getContentResolver().query(contactData4, null, null, null, null);
         c4.moveToFirst();
         String cNumber = c4.getString(c4.getColumnIndex("data1"));
         TextView tv4;
         tv4 = (TextView)findViewById(R.id.number4);
         tv4.setText(cNumber);
    }


}

Answers


What exactly isn't working?

And you're doing 4 times the same thing as you can see more clearly here:

if (resultCode == Activity.RESULT_OK) 
{
     Uri contactData = intent.getData();
     Cursor cursor = getContentResolver().query(contactData1, null, null, null, null);
     cursor.moveToFirst();
     String contactNumber = cursor.getString(cursor.getColumnIndex("data1"));

     TextView tv1 = (TextView)findViewById(R.id.number1);
     tv1.setText(contactNumber);
     TextView tv2 = (TextView)findViewById(R.id.number2);
     tv2.setText(contactNumber);
     TextView tv3 = (TextView)findViewById(R.id.number3);
     tv3.setText(contactNumber);
     TextView tv4 = (TextView)findViewById(R.id.number4);
     tv4.setText(contactNumber);
}

I just shortened it. It's the same code actually. This way you will have the same phonenumber in every TextView.

EDIT:

This could be a solution.

To distinguish between the several TextViews you should declare some final request constants

public static final int CONTACT_PICKER_REQUEST_FIRST = 0, CONTACT_PICKER_REQUEST_SECOND = 1, CONTACT_PICKER_REQUEST_THIRD = 2, CONTACT_PICKER_REQUEST_FOURTH = 3;

In your onClick method of the buttons put something like this:

public void onClick(View v) {
    int request_code = -1;

    switch(v.getId()) {
        case R.id.number1:
            request_code= CONTACT_PICKER_RESULT_FIRST;
            break;
        case R.id.number2:
            request_code= CONTACT_PICKER_RESULT_SECOND;
            break;
        case R.id.number3:
            request_code= CONTACT_PICKER_RESULT_THIRD;
            break;
        case R.id.number4:
            request_code= CONTACT_PICKER_RESULT_FOURTH;
            break;
    }

     Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_UR);
    startActivityForResult(contactPickerIntent, request_code);
}

And finally in your onActivityResult() something like this should work:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { TextView toFill;

        switch (requestCode) {
        case CONTACT_PICKER_REQUEST_SECOND:
            toFill = (TextView) findViewById(R.id.number2);
            break;
        case CONTACT_PICKER_REQUEST_THIRD:
            toFill = (TextView) findViewById(R.id.number3);
            break;
        case CONTACT_PICKER_REQUEST_FOURTH:
            toFill = (TextView) findViewById(R.id.number4);
            break;
        default:
            toFill = (TextView) findViewById(R.id.number1);
            break;
        }

        Uri uri = data.getData();
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        toFill.setText(phoneNumber);
    }
}

There might be some typos as I wrote it down just here and not in my ADT..


Just an update, @Endzeit has given me a great help. In my case, I have 4 EditTexts to fill in. Each EditText has its own getContact button associated with it. So I have decided to share my working code for the ones who have this case:

protected void onActivityResult(int reqCode, int resultCode, Intent intent) 
{
    super.onActivityResult(reqCode, resultCode, intent);
    switch (reqCode) {
    case ADDRESS_REQUEST2:
        EditText edittext2 = (EditText) findViewById(R.id.number2);
        Uri uri2 = intent.getData();
        Cursor cursor2 = getContentResolver().query(uri2, null, null, null, null);
        cursor2.moveToFirst();
        String phoneNumber2 = cursor2.getString(cursor2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        edittext2.setText(phoneNumber2);
        break;
    case ADDRESS_REQUEST3:
        EditText edittext3 = (EditText) findViewById(R.id.number3);
        Uri uri3 = intent.getData();
        Cursor cursor3 = getContentResolver().query(uri3, null, null, null, null);
        cursor3.moveToFirst();
        String phoneNumber3 = cursor3.getString(cursor3.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        edittext3.setText(phoneNumber3);
        break;
    case ADDRESS_REQUEST4:
        EditText edittext4 = (EditText) findViewById(R.id.number4);
        Uri uri4 = intent.getData();
        Cursor cursor4 = getContentResolver().query(uri4, null, null, null, null);
        cursor4.moveToFirst();
        String phoneNumber4 = cursor4.getString(cursor4.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        edittext4.setText(phoneNumber4);
        break;
    default:
        EditText edittext1 = (EditText) findViewById(R.id.number1);
        Uri uri1 = intent.getData();
        Cursor cursor1 = getContentResolver().query(uri1, null, null, null, null);
        cursor1.moveToFirst();
        String phoneNumber1 = cursor1.getString(cursor1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        edittext1.setText(phoneNumber1);
        break;
    }

Need Your Help

How to get the values of elements inside the <td> element of a gridview control using javascript?

javascript jquery html asp.net css

I need some help for some issues that I want to tuckle some issues about using javascript. I was using gridview control to display all my comments that are saved from the database. What I want is w...

specific number of characters string in php

php string

I want to generate specific characters string. I need to generate exact 35 characters string for that I am using this function.