简体   繁体   中英

Android:How to send more than 1 variable with Intent?

Am sending a few variables from main.java :

    Bundle bund = new Bundle();
    Intent intent = new Intent(this, newWindow.class);
    String name = editName.getText().toString();
    bund.putString(yourName, name);
    String pass= editPassword.getText().toString();
    bund.putString(yourPass,pass);
    String mail=EditMail.getText().toString();
    bund.putString(yourMail,mail);
    intent.putExtras(bund);
    startActivity(intent);

to newWindow.java :

        Intent intent = getIntent();
        Bundle extras =  intent.getExtras();
           String name = extras.getString(main.yourName);
           String mail = extras.getString(main.yourMail);
           String pass = extras.getString(main.yourPass);
        viewText1 = (TextView) findViewById(R.id.textView2);
        viewText2 = (TextView) findViewById(R.id.textView3);
        viewText3 = (TextView) findViewById(R.id.textView4);
        viewText1.setText(name);
        viewText2.setText(mail);
        viewText3.setText(pass);

The problem is, only last variable is passed and showed in all 3 textview's

Presumably, you are using the same key in all cases. In other words, your value of yourName is the same as the value of yourPass , which is the same as the value of yourMail . These need to have unique values. Usually, we use static final String keys for Bundle .

Why not avoid the Bundle part ?

Simply in the FirstActivity, try this

Intent intent = new Intent(this, NewWindow.class);
String name = editName.getText().toString();
String pass= editPassword.getText().toString();
String mail=EditMail.getText().toString();
intent.putExtra("name",name);
intent.putExtra("mail",mail);
intent.putExtra("pass",pass);
startActivity(intent);

Then in RecievingActivity do this

Intent extras=getIntent();
String name = extras.getStringExtra("name");
String mail = extras.getStringExtra("mail");
String pass = extras.getStringExtra("pass");
viewText1.setText(name);
viewText2.setText(mail);
viewText3.setText(pass);

Let me know if it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM