1

Helllo Guys.

I'm having issues when I want to get strings from my ContactView into my MainActivity.

I tried to create an Intent in my ContactView and retrieve the data from this intent in my MainAcitivty.

The Problem I have is that it opens the MainActivity when I do anything in my ContactView:

What I did is this:

ContactView.java

     //this is where the Activity gets opend right?
      Intent getContact = new Intent(this, MainActivity.class); 
      getContact.putExtra("Contact",phoneNo);
      startActivity(getContact);

and this is what i do to retrieve it:

 Bundle extras = getIntent().getExtras();
 if (extras != null) {
     //this is where i should get the contact?
     String readContact= extras.getString("Contact")
 }

I have a contactPicked method in my ContactView that gets me the contactname and phonenumber and thats actually what i want to pass to my MainActivity because there i have a button that should do an sendsms with text and contact from ContactView.

This is my contactPicked method:

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

            // getData() method will have the Content Uri of the selected contact
            Uri uri = data.getData();

            //Query the content uri
            cursor = getContentResolver().query(uri, null, null, null, null);
            cursor.moveToFirst();
            // column index of the phone number
            int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            // column index of the contact name
            int nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            phoneNo = cursor.getString(phoneIndex);
            name = cursor.getString(nameIndex);
            // Set the value to the textviews
            textView1.setText(name);
            textView2.setText(phoneNo);
        } catch (Exception e) {
            e.printStackTrace();
        }

Do I need to put the intent into my method? Eventhough it will open me the Activity after I've selected a contact.

Am I missing something that I can't find?

Can anyone help me please?

###EDIT

This is my onClick for the button to pick contacts:

public void onClick(View v) {
    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
    startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);
}

###EDIT 2

The Orders of my Activites are MainActivity(MapView) that ContactView(where I pick contacts and display them)

I want still to pick Contacts and display them in my ContactView but when I am in my MainActivity again I have a button that should get the strings from ContactView and do something with them like sending a sms.

ContactView.java

public class ContactView extends AppCompatActivity {
private static final int RESULT_PICK_CONTACT = 85;
private TextView textView1;
private TextView textView2;
private TextView message;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contact_view);
    textView1 = (TextView) findViewById(R.id.TxtName);
    textView2 = (TextView) findViewById(R.id.TxtNumber);
    message = (TextView) findViewById(R.id.message);

    //speicher den aktuellen status der activity
    SharedPreferences settings = getSharedPreferences("SelectedContact", MODE_PRIVATE);
    String name = settings.getString("contactName", "");
    //the second parameter set a default data if “contactName” is empty
    if (!name.isEmpty()){
        textView1.setText(name);
    }
    String phoneNo = settings.getString("contactPhone", "");//the second parameter set a default data if “contactName” is empty
    if (!phoneNo.isEmpty()){
        textView2.setText(phoneNo);
    }
}

public void onClick(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) {
    // check whether the result is ok
    if (resultCode == RESULT_OK) {
        // Check for the request code, we might be usign multiple startActivityForReslut
        switch (requestCode) {
            case RESULT_PICK_CONTACT:
                contactPicked(data);
                break;
        }
    } else {
        Log.e("ContactView", "Failed to pick contact");
    }
}

/**
 * Query the Uri and read contact details. Handle the picked contact data.
 *
 * @param data
 */
private void contactPicked(Intent data) {
    Cursor cursor = null;
    try {
        String phoneNo = null;
        String name = null;
        String msg=message.getText().toString();
        //String ResqMessage = null;
        // getData() method will have the Content Uri of the selected contact
        Uri uri = data.getData();

        //Query the content uri
        cursor = getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        // column index of the phone number
        int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
        // column index of the contact name
        int nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        phoneNo = cursor.getString(phoneIndex);
        name = cursor.getString(nameIndex);
        // Set the value to the textviews
        textView1.setText(name);
        textView2.setText(phoneNo);
        SharedPreferences settings = getSharedPreferences("SelectedContact", MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("contactName", name);
        editor.putString("contactPhone", phoneNo);
        editor.commit();

    } catch (Exception e) {
        e.printStackTrace();
    }
}
bfmv991
  • 109
  • 1
  • 12
  • Can you specify the activities order and the communication between them? MainActivity is the first? – ezefire Oct 29 '15 at 09:10
  • are you sure you are getting the contact name and number when you are setting it to textview? – Arslan Ashraf Oct 29 '15 at 09:22
  • yes i get them i just set them there so they are display in my view – bfmv991 Oct 29 '15 at 09:37
  • Please post the ContactView class code – ezefire Oct 29 '15 at 09:38
  • But where are you calling this intent: Intent getContact = new Intent(this, MainActivity.class); getContact.putExtra("Contact",phoneNo); startActivity(getContact);?? – ezefire Oct 29 '15 at 10:16
  • I suggest that you should organize and clean your question explaining better how it works and what is your issue to let us understand a bit better so we can help you. – ezefire Oct 29 '15 at 10:26

3 Answers3

2

Actually You receive wrong way. Look at your code, you do not pass any Bundle with your MainActivity, so dont need to call getExtras() before get your passed contact. You can do it as below from MainActivity:

//start main
Intent getContact = new Intent(this, MainActivity.class); 
getContact.putExtra("Contact",phoneNo);
startActivity(getContact);
// In MainActivity onCreate
String contact = getIntent().getStringExtra("Contact");
Kingfisher Phuoc
  • 8,052
  • 9
  • 46
  • 86
  • Dont i need to this in ContactView?: Intent getContact = new Intent(this, MainActivity.class); getContact.putExtra("Contact",phoneNo); startActivity(getContact); and this in my MainActivity withing the onCreate: String contact = intent.getStringExtra("Contact"); and i have no phoneNo in my MainActivity only in ContactView – bfmv991 Oct 29 '15 at 09:35
  • what's your `ContactView`? As I see in your code, `contactView` is not yours, it's system contact view right (Intent.ACTION_PICK)? If so, how you can call start `MainActivity` in contactView? – Kingfisher Phuoc Oct 29 '15 at 09:48
  • Added the ContactView ... actually its a class – bfmv991 Oct 29 '15 at 09:55
  • yeah, I saw it. So, if you start `MainActivity` and get phone number in `onCreate` as above, the code will work. There's no point to use `SharePreference` here! – Kingfisher Phuoc Oct 29 '15 at 10:00
  • so tell me where you commented start main .. do i write this in my MainActivity? how do i get there a string thts phoneNo?? shall i create an empty string for this? and than i do the rest in on create?? – bfmv991 Oct 29 '15 at 10:02
  • Bundle extras = getIntent().getExtras(); if (extras != null) { //this is where i should get the contact? String readContact= extras.getString("Contact") } and where do i do this? – bfmv991 Oct 29 '15 at 10:02
  • no! If you start `MainActivity` like this: `Intent getContact = new Intent(this, MainActivity.class); getContact.putExtra("Contact",phoneNo); startActivity(getContact);`, just call `String contact = getIntent().getStringExtra("Contact");` in your `MainActivity` class. Why getExtras when you dont pass any `Bundle` in your `getContact` intent? The getExtras will always return null in this case! – Kingfisher Phuoc Oct 29 '15 at 10:06
  • Ok and this will pass me the strings from ContactView? sry for beeing dumb but i dont get it :( – bfmv991 Oct 29 '15 at 10:08
  • The getExtras() in MainActivity only work when you start your `getContact` when you start your `getContact` with bundle: `getContact.putExtras(new Bundle());` – Kingfisher Phuoc Oct 29 '15 at 10:11
0

Are you using startActivityForResult in your MainActivity?

Normally you call startActivityForResult(..) in your first activity and handle the results later on in a onActivityResult method (in your MainActivity). For doing this you have to set the results in your second Activity via setResult(intent i).

Are you doing this? If not, search for it or i can give you some code..

Stephan Huewe
  • 124
  • 1
  • 7
  • No i have no startActivityForResult or onActivityResult in my MainActivity only in my ContactView when i load the Contact – bfmv991 Oct 29 '15 at 09:32
  • Don't you want to use it for some reason or don't you know it? If you want to use it, here is an example how to use it: http://stackoverflow.com/questions/22553672/android-startactivityforresult-setresult-for-a-view-class-and-an-activity-cla – Stephan Huewe Oct 29 '15 at 09:40
0

You should use this code for parse

  Intent intent = new Intent(this,FirstActivity.class);
    Bundle bundle = new Bundle();
    bundle.putString("data","value");
    intent.putExtras(bundle);
    startActivity(intent);

and for get data in SecondActivity use this code:

Bundle bundle = getIntent().
bundle.getString("data");
Ahmad Azarnia
  • 129
  • 1
  • 1
  • 11