-1

I am trying to send parameters from one Activity to another using Intent and extras.putString(); from one activity and trying to fetch the values in another and set that value in the Textview field.

My code looks like this:

PAActivity.java

Fname = (EditText) findViewById(R.id.editFirst);
Lname = (EditText) findViewById(R.id.editLast);
email = (EditText) findViewById(R.id.editEmail);
phone = (EditText) findViewById(R.id.editPhone);
submit = (Button) findViewById(R.id.submit);
Fnameholder = Fname.getText().toString();
Lnameholder = Lname.getText().toString();
emailHolder = email.getText().toString();
phoneHolder = phone.getText().toString();

Log.e("phoneHolder",phoneHolder);

submit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent myIntent = new Intent(PAActivity.this,DisplayActivity.class);
        Bundle extras = new Bundle();
        extras.putString("F_NAME", Fnameholder);
        extras.putString("L_NAME", Lnameholder);
        extras.putString("EMAIL", emailHolder);
        extras.putString("PHONE", phoneHolder);
        myIntent.putExtras(extras);
        PAActivity.this.startActivity(myIntent);
    }
});

DisplayActivity.java

name = new TextView(this);
email = new TextView(this);
phone = new TextView(this);
name = (TextView) findViewById(R.id.name);
email = (TextView) findViewById(R.id.email);
phone = (TextView) findViewById(R.id.phone);
Bundle extras = getIntent().getExtras();

if (extras != null) {
    first = (String) extras.get("F_NAME");
    last = (String) extras.get("L_NAME");
    Nameholder = "my name is "+first+" "+last;
    name.setText(Nameholder);
    emailHolder = (String) extras.get("EMAIL");
    email.setText(emailHolder);
    phoneHolder = (String) extras.get("PHONE");
    phone.setText(phoneHolder);
}

The problem here is that, the intent is loading the second activity but the setText does not seem to work. I am not able to see the values being set in the Textviews. Could someone please help?

Michael
  • 3,093
  • 7
  • 39
  • 83
Dhirish
  • 31
  • 1
  • 1
  • 9
  • What if you use `extras.getString()` instead of `(String) extras.get()`? And does a `extra.hasKey()` check return `true`? – Michael Dodd Mar 21 '18 at 20:40
  • Also the `new TextView(this);` lines are unnecessary as you're re-declaring them with `findViewById()` immediately afterwards. – Michael Dodd Mar 21 '18 at 20:42
  • Try to use `putExtra()` method on Intent directly, and `getStringExtra()` to get the String. So `intent.putExtra(someString)` and `someString = intent.getStringExtra()`. – J K Mar 21 '18 at 20:48
  • In DisplayActivity.java, remove the first 3 lines. You don't need to instantiate a new TextView or any kind of view. You do it correctly by using `findviewById` – Michael Mar 21 '18 at 20:54

3 Answers3

2

In your submit.onClick() function you should pull the values of the EditTexts again. You want the last value to be sent when the User hits submit. I think you might be putting in an empty string because you're not pulling the values in your onClick

1

As mentioned in some other answers, you need to get the values of the EditTexts inside of your onclick method. Your PAActivity should now look like this:

/* Depending on what the rest of your code is, 
* you may be able to remove the lines of code leading up to the 
* `submit.setOnclickListener`, and only have it inside of the `onClick` method.
*/

Fname = (EditText) findViewById(R.id.editFirst);
Lname = (EditText) findViewById(R.id.editLast);
email = (EditText) findViewById(R.id.editEmail);
phone = (EditText) findViewById(R.id.editPhone);
submit = (Button) findViewById(R.id.submit);
Fnameholder = Fname.getText().toString();
Lnameholder = Lname.getText().toString();
emailHolder = email.getText().toString();
phoneHolder = phone.getText().toString();

Log.e("phoneHolder", phoneHolder);

submit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Fname = (EditText) findViewById(R.id.editFirst);
        Lname = (EditText) findViewById(R.id.editLast);
        email = (EditText) findViewById(R.id.editEmail);
        phone = (EditText) findViewById(R.id.editPhone);
        submit = (Button) findViewById(R.id.submit);
        Fnameholder = Fname.getText().toString();
        Lnameholder = Lname.getText().toString();
        emailHolder = email.getText().toString();
        phoneHolder = phone.getText().toString();

        Intent myIntent = new Intent(PAActivity.this,DisplayActivity.class);
        Bundle extras = new Bundle();
        extras.putString("F_NAME", Fnameholder);
        extras.putString("L_NAME", Lnameholder);
        extras.putString("EMAIL", emailHolder);
        extras.putString("PHONE", phoneHolder);
        myIntent.putExtras(extras);
        PAActivity.this.startActivity(myIntent);
    }
});

Also, as I mentioned in a comment on your question, this is what your new DisplayActivity.java should look like:

name = (TextView) findViewById(R.id.name);
email = (TextView) findViewById(R.id.email);
phone = (TextView) findViewById(R.id.phone);
Bundle extras = getIntent().getExtras();

if (extras != null) {
    first = (String) extras.get("F_NAME");
    last = (String) extras.get("L_NAME");
    Nameholder = "my name is " + first + " " + last;
    name.setText(Nameholder);
    emailHolder = (String) extras.get("EMAIL");
    email.setText(emailHolder);
    phoneHolder = (String) extras.get("PHONE");
    phone.setText(phoneHolder);
}
Michael
  • 3,093
  • 7
  • 39
  • 83
0

You need to fetch the values in the onClick method from the EditText views.

If you do not fetch the value in the onClick then it will pass the default values to DisplayActivity.java, which is what is happening to you.

Michael
  • 3,093
  • 7
  • 39
  • 83
Fahad
  • 37
  • 1
  • 10