LisaH
LisaH

Reputation: 173

Android/Java passing value from one screen to another and displaying value

I am a beginner, and having trouble. I have two pages. On logon.java user enters name and clicks button. On second screen I need to have "Hello"+ the name they entered on the first page. So for the first page I have:

  public class LogonActivity extends Activity
{ 
private EditText enterName;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    //associate view with activity
    setContentView(R.layout.logon);
    //set the button
    Button button=(Button)findViewById(R.id.button);
    //access the field where the user entered the name
    final EditText editTextName= (EditText)findViewById(R.id.enterName);

    //button listener
    button.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            //save the editTextName to a new variable -enterName
            String enterName=editTextName.getText().toString(); 

            //create explicit intent
    Intent nameResultsIntent = new 
Intent(getApplicationContext(), 
ConfirmationActivity.class);
            //pass that to next activity
            nameResultsIntent.putExtra("PERSON_NAME",enterName);
            startActivity(nameResultsIntent);
        }
    });
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.logon_main, menu);
    return true;

On the second page is: public class ConfirmationActivity extends Activity {

EditText enterName;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    //associate the layout with this activity using setContentView

    setContentView(R.layout.confirmation);

    //get the name the user entered
    String enterName=getIntent().getStringExtra("PERSON_NAME");

    findViewById(R.id.confirmationText);

    String confirmationText = enterName;
    //confirmationText.setText(enterName);
    finish();
}

}

}

}

So... its the last lines on the ConfirmationPage.

The Text field on the 1st page is : android:id="@+id/enterName" The field on the 2nd page where I want the text to appear is :

Can someone help with the last line to show the text on the second page?

Upvotes: 0

Views: 787

Answers (1)

Mike B
Mike B

Reputation: 5451

Try something like this

//get the name the user entered
String enterName=getIntent().getStringExtra("PERSON_NAME");

EditText confirmationText = (EditText)findViewById(R.id.confirmationText);

confirmationText.setText(enterName);
finish();

Upvotes: 2

Related Questions