0

So, still working on the contacts portion of this code. I decided it was time to just show off my file. I am trying to select three contacts out of a contact list and make the name and number of said contacts show up on the screen. I manage to get one of them to work but as soon as I pick the second contact the name and number of the first is changed to that of the second while the second contact's info stay's blank. How do I make it that all of them show up where they need to be. Here is the code:

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class contacts extends Activity {

TextView num1, num2, num3, name1, name2, name3;
Button saveNums, pick1Btn, pick2Btn, pick3Btn;
Context c = this;
private final static int REQUEST_CONTACTPICKER = 1;
public static String filename = "sharedString";
SharedPreferences prefs;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.contactsettings_layout);
    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    uiBinder();
    btnManger();
}

public void uiBinder(){
    name1 = (TextView) findViewById(R.id.iceName1);
    name2 = (TextView) findViewById(R.id.iceName2);
    name3 = (TextView) findViewById(R.id.iceName3);
    num1 = (TextView) findViewById(R.id.iceNum1);
    num2 = (TextView) findViewById(R.id.iceNum2);
    num3 = (TextView) findViewById(R.id.iceNum3);
    saveNums = (Button) findViewById(R.id.saveICEBtn);
    pick1Btn = (Button) findViewById(R.id.contactPickBtn);
    pick2Btn = (Button) findViewById(R.id.contact2PickBtn);
    pick3Btn = (Button) findViewById(R.id.contact3PickBtn);
}

private void selectContact()
{
    // This intent will fire up the contact picker dialog
    Intent intent = new Intent(Intent.ACTION_PICK,
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
    startActivityForResult(intent, REQUEST_CONTACTPICKER);
}
public void btnManger(){

    pick1Btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            selectContact();
        }
    });

    pick2Btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            selectContact();
        }
    });

    pick3Btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            selectContact();
        }
    });
}

public void onActivityResult(int reqCode, int resultCode, Intent data) {

    super.onActivityResult(reqCode, resultCode, data);

    switch (reqCode) {
        case (1) :
            if (resultCode == Activity.RESULT_OK) {
                Uri contactData = data.getData();
                Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
                cursor.moveToFirst();
                String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME));
                String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
                name1.setText("Contact name: " + name);
                num1.setText("Contact number: " + number);
            }
            break;
        case (2):
            if (resultCode == Activity.RESULT_OK) {
                Uri contactData = data.getData();
                Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
                cursor.moveToFirst();
                String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME));
                String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
                name2.setText("Contact name: " + name);
                num2.setText("Contact number: " + number);
            }
            break;
        case (3):
            if (resultCode == Activity.RESULT_OK) {
                Uri contactData = data.getData();
                Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
                cursor.moveToFirst();
                String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME));
                String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
                name3.setText("Contact name: " + name);
                num3.setText("Contact number: " + number);
            }
            break;
    }
}
}

Thanks again, - Alx

CodeMonkeyAlx
  • 813
  • 4
  • 16
  • 32

1 Answers1

1

Here is your code except that i changed the onclick to be 1 onclick handler rather than 3 that call the same method and it uses the btn id to send the request code and we check that in your switch case

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class contacts extends Activity {

TextView num1, num2, num3, name1, name2, name3;
Button saveNums, pick1Btn, pick2Btn, pick3Btn;
Context c = this;
public static String filename = "sharedString";
SharedPreferences prefs;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.contactsettings_layout);
    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    uiBinder();
    btnManger();
}

public void uiBinder(){
    name1 = (TextView) findViewById(R.id.iceName1);
    name2 = (TextView) findViewById(R.id.iceName2);
    name3 = (TextView) findViewById(R.id.iceName3);
    num1 = (TextView) findViewById(R.id.iceNum1);
    num2 = (TextView) findViewById(R.id.iceNum2);
    num3 = (TextView) findViewById(R.id.iceNum3);
    saveNums = (Button) findViewById(R.id.saveICEBtn);
    pick1Btn = (Button) findViewById(R.id.contactPickBtn);
    pick2Btn = (Button) findViewById(R.id.contact2PickBtn);
    pick3Btn = (Button) findViewById(R.id.contact3PickBtn);
}

public void btnManger(){

    View.OnClickListener click = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
    // This intent will fire up the contact picker dialog
    Intent intent = new Intent(Intent.ACTION_PICK,
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
    startActivityForResult(intent, view.getId());
        }
    };

    pick1Btn.setOnClickListener(click);

    pick2Btn.setOnClickListener(click);

    pick3Btn.setOnClickListener(click);
}

public void onActivityResult(int reqCode, int resultCode, Intent data) {

    super.onActivityResult(reqCode, resultCode, data);

    switch (reqCode) {
        case (R.id.contactPickBtn) :
            if (resultCode == Activity.RESULT_OK) {
                Uri contactData = data.getData();
                Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
                cursor.moveToFirst();
                String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME));
                String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
                name1.setText("Contact name: " + name);
                num1.setText("Contact number: " + number);
            }
            break;
        case (R.id.contactPick2Btn):
            if (resultCode == Activity.RESULT_OK) {
                Uri contactData = data.getData();
                Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
                cursor.moveToFirst();
                String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME));
                String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
                name2.setText("Contact name: " + name);
                num2.setText("Contact number: " + number);
            }
            break;
        case (R.id.contactPick3Btn):
            if (resultCode == Activity.RESULT_OK) {
                Uri contactData = data.getData();
                Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
                cursor.moveToFirst();
                String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME));
                String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
                name3.setText("Contact name: " + name);
                num3.setText("Contact number: " + number);
            }
            break;
    }
}
}

P.S. I wrote this here in the browser you should understand from this it may not compaile but should be enough for you to understand

Shereef Marzouk
  • 3,282
  • 7
  • 41
  • 64
  • Right now I am just trying to work on figuring out how to get the cases to work right, everything else is peachy but for every one of the pick#Btn.getID()'s in there I have an error coming up saying it needs a constant expression. – CodeMonkeyAlx Jun 30 '14 at 19:08
  • Scratch that I got it. Works just the same with R.id.buttonIdHere. Thanks man! :) – CodeMonkeyAlx Jun 30 '14 at 19:23