简体   繁体   中英

How Do i Get all the Values When Sending String Values from one Activity to Another in Android Studio 3.4

Please am designing an android app, i want to send (5)String values from one activity to another activity to use in different TextViews, i have tried virtually all the code i could find online on the topic, but i keep getting just one value(the last value i send in the putExtra()). please i am new to Android Studio and will appreciate every help.

I have used the putExtra() to send one data to another activity and it worked perfectly, while trying to do the same with multiple data i keep getting just one of the data sent. I have also tried using a bundle object, to receive the data from the other(recieving) activity.

I expect getting all this data ( intent.putExtra("surname", "Jerry").
intent.putExtra("middlename", "chris"). intent.putExtra("lastname", "Enema")) in another activity, but i keep getting just "Enema" alone

this is my code; //in the firstActivity

     send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String sFirstname = firstname.getText().toString();
                String sLastname = lastname.getText().toString();

                Intent intent = new Intent(MainActivity.this, ReceiveActivity.class);

                intent.putExtra("surname" ,sFirstname);
                intent.putExtra("lastname", sLastname);

                startActivity(intent);

            }
        });

//And In the second Activity

firstname = findViewById(R.id.firstname); lastname = findViewById(R.id.firstname);

Intent intent = getIntent();
Bundle bundle = getIntent().getExtras();

String ssurname = bundle.getString("surname");
String slastname = bundle.getString("lastname");

firstname.setText(ssurname);
lastname.setText(slastname);

I would prefer not to ue getExtras so you should have two getExtra Like this :

 Intent intent = getIntent();

 String ssurname = intent.getExtra("surname");
 String slastname = intent.getExtra("lastname");

 firstname.setText(ssurname);
 lastname.setText(slastname);

Try this. In First Activity:

String sFirstname = "Tope";
String sLastname = "Adebodun";

Intent theIntent = new Intent(MainActivity.this, ReceivingActivity.class);
theIntent.putExtra("firstname", sFirstname);
theIntent.putExtra("lastname", sLastname);

Then in your second activity, do this in the onCreate method:

Intent intent = getIntent();
String thefirst = (String) intent.getExtras.getString("firstname");
String thelast = (String) intent.getExtras.getString("lastname");

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