How to pass two Strings from one Activity to anoth

2019-09-14 12:48发布

问题:

I know how to pass one string from one activity to another, but how would you do this for two strings "nameString" and "addressString"?

add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            addressString = address.getText().toString();
            nameString = name.getText().toString();
            Intent intent = new Intent(AddLocationActivity.this, MapsActivity.class);
            intent.putExtra("address", addressString);
            AddLocationActivity.this.startActivity(intent);
        }
    });

回答1:

this way you can pass and retrive string from one activity to another activity

Intent intent = new Intent(AddLocationActivity.this, MapsActivity.class);
intent.putExtra("address", addressString);
intent.putExtra("string2", string2);
startActivity(intent);

retrive String to MapsActivity

Intent intent = getIntent();
String address = intent.getStringExtra("address");
String string2 intent.getStringExtra("string2");

Hope it will help you :)



回答2:

Similar the way u passed 1st string u can pass 'n' number of strings via intent.

Intent intent = new Intent(AddLocationActivity.this, MapsActivity.class);
intent.putExtra("address", addressString);
intent.putExtra("string2", string2);
AddLocationActivity.this.startActivity(intent);


回答3:

To pass:

add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            addressString = address.getText().toString();
            nameString = name.getText().toString();
            Intent intent = new Intent(AddLocationActivity.this, MapsActivity.class);
            intent.putExtra("name",nameString);
            intent.putExtra("address", addressString);
            AddLocationActivity.this.startActivity(intent);
        }
    });
}

To get:

Intent intent = getIntent();
String address = intent.getStringExtra("name");
String string2 intent.getStringExtra("address");