[英]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()). 请设计一个Android应用程序,我想将(5)字符串值从一个活动发送到另一个活动以在不同的TextView中使用,我已经尝试了几乎可以在该主题上在线找到的所有代码,但我一直只获得一个值(我在putExtra()中发送的最后一个值)。 please i am new to Android Studio and will appreciate every help. 请我是Android Studio的新手,将不胜感激。
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. 我已经使用putExtra()将一个数据发送到另一活动,并且它工作得很好,同时尝试对多个数据执行相同的操作,但我一直只收到发送的数据之一。 I have also tried using a bundle object, to receive the data from the other(recieving) activity. 我也尝试过使用bundle对象,以从其他(接收)活动中接收数据。
I expect getting all this data ( intent.putExtra("surname", "Jerry"). 我希望得到所有这些数据(intent.putExtra(“ surname”,“ Jerry”)。
intent.putExtra("middlename", "chris"). intent.putExtra(“ middlename”,“ chris”)。 intent.putExtra("lastname", "Enema")) in another activity, but i keep getting just "Enema" alone intent.putExtra(“ lastname”,“ Enema”))在另一项活动中,但我一直独自获得“ Enema”
this is my code; 这是我的代码; //in the firstActivity //在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); 名字= findViewById(R.id.firstname); lastname = findViewById(R.id.firstname); 姓= 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 : 我不想使用ue getExtras
所以您应该有两个getExtra
像这样:
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: 然后在第二个活动中,在onCreate方法中执行以下操作:
Intent intent = getIntent();
String thefirst = (String) intent.getExtras.getString("firstname");
String thelast = (String) intent.getExtras.getString("lastname");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.