I have 2 Activity. I get the text by:
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Saved Directory!",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(NewCard.this, CardBox.class);
EditText txtName = (EditText) findViewById(R.id.txtName);
EditText txtCompany= (EditText) findViewById(R.id.txtCompany);
EditText txtPhone= (EditText) findViewById(R.id.txtPhone);
EditText txtMobile = (EditText) findViewById(R.id.txtMobile);
EditText txtAddress = (EditText) findViewById(R.id.txtAddress);
EditText txtEmail = (EditText) findViewById(R.id.txtEmail);
EditText txtWebsite = (EditText) findViewById(R.id.txtWebsite);
EditText txtTitle = (EditText) findViewById(R.id.txtTitle);
String Name=txtName.getText().toString(),
Company=txtCompany.getText().toString(),
Phone=txtPhone.getText().toString(),
Mobile=txtMobile.getText().toString(), Address=txtAddress.getText().toString(),
Email=txtEmail.getText().toString(), Website=txtWebsite.getText().toString(),
Title=txtTitle.getText().toString();
intent.putExtra(EXTRA_MESSAGE, Name);
intent.putExtra(EXTRA_MESSAGE, Company);
intent.putExtra(EXTRA_MESSAGE, Phone);
intent.putExtra(EXTRA_MESSAGE, Mobile);
intent.putExtra(EXTRA_MESSAGE, Address);
intent.putExtra(EXTRA_MESSAGE, Email);
intent.putExtra(EXTRA_MESSAGE, Website);
intent.putExtra(EXTRA_MESSAGE, Title);
startActivity(intent);
}
});
and i set it on textView by:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_card_box);
// Get the message from the intent
Intent intent;
intent = getIntent();
String Name = intent.getStringExtra(NewCard.EXTRA_MESSAGE);
String Company = intent.getStringExtra(NewCard.EXTRA_MESSAGE);
String Phone = intent.getStringExtra(NewCard.EXTRA_MESSAGE);
String Mobile = intent.getStringExtra(NewCard.EXTRA_MESSAGE);
String Address = intent.getStringExtra(NewCard.EXTRA_MESSAGE);
String Email = intent.getStringExtra(NewCard.EXTRA_MESSAGE);
String Website = intent.getStringExtra(NewCard.EXTRA_MESSAGE);
String Title = intent.getStringExtra(NewCard.EXTRA_MESSAGE);
//Display the message in textview
TextView stv1 = (TextView) findViewById(R.id.stv1);
TextView stv2 = (TextView) findViewById(R.id.stv2);
TextView stv3 = (TextView) findViewById(R.id.stv3);
TextView stv4 = (TextView) findViewById(R.id.stv4);
TextView stv5 = (TextView) findViewById(R.id.stv5);
TextView stv6 = (TextView) findViewById(R.id.stv6);
TextView stv7 = (TextView) findViewById(R.id.stv7);
TextView stv8 = (TextView) findViewById(R.id.stv8);
stv1.setText(Name);
stv2.setText(Company);
stv3.setText(Phone);
stv4.setText(Mobile);
stv5.setText(Address);
stv6.setText(Email);
stv7.setText(Website);
stv8.setText(Title);
Finally when i clicked 'Save' button. New Activity opens and i see all of the textView are set to stv8.setText(Title).
What is wrong? There is no error!
All your keys are the same
NewCard.EXTRA_MESSAGE
. That is why all strings from name to title are the same. You need different keysThen you have
What you need in
NewCard
Then
intent.putExtra(EXTRA_NAME, Name); intent.putExtra(EXTRA_COMPANY, Company); ... // similarly for others Then in SecondActivity
I guess you followed
http://developer.android.com/training/basics/firstapp/starting-activity.html
But you also need different keys
public Intent putExtra (String name, String value)
Add extended data to the intent. The name must include a package prefix, for example the app com.android.contacts would use names like (Example)
"com.android.contacts.ShowAll".
Parameters
Returns the same Intent object, for chaining multiple calls into a single statement.