如何使用Android Studio中的捆绑包将数据发送到下一个活动
这是详细活动...我正在使用意图中的putextra将此活动发送到下一个活动
public class Detail_Activity extends AppCompatActivity {
ImageView ivImage2;
TextView txtDescription, txtName, txtPrice;
String key = "";
String imageUrl = "";
Button delete_Recipe, update_Recipe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
ivImage2 = (ImageView)findViewById(R.id.ivImage2);
txtDescription = (TextView) findViewById(R.id.txtDescription);
txtName = (TextView) findViewById(R.id.txtName);
txtPrice = (TextView) findViewById(R.id.txtPrice);
delete_Recipe = (Button) findViewById(R.id.delete_Recipe);
update_Recipe = (Button) findViewById(R.id.update_Recipe);
update_Recipe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(Detail_Activity.this, Upload_Recipe.class)
.putExtra("recipeNameKey", txtName.getText().toString())
.putExtra("recipeDescriptionKey", txtDescription.getText().toString())
.putExtra("recipePriceKey", txtPrice.getText().toString())
.putExtra("oldimageUrl", imageUrl)
.putExtra("key",key)
);
}
});
**这是更新活动...在此活动中,数据未显示或不显示显示收到的数据,而代码正确**
public class Update_Activity extends AppCompatActivity {
ImageView update_Recipe_Image;
EditText update_Recipe_Name, update_Recipe_Description, update_Recipe_Price;
Button updatebutton;
Uri uri;
ActivityResultLauncher<String> mContent;
String imageUrl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update);
update_Recipe_Image = (ImageView) findViewById(R.id.update_Recipe_Image);
update_Recipe_Name = (EditText) findViewById(R.id.update_Recipe_Name);
update_Recipe_Description = (EditText) findViewById(R.id.update_Recipe_Description);
update_Recipe_Price = (EditText) findViewById(R.id.update_Recipe_Price);
updatebutton = (Button) findViewById(R.id.updatebutton);
Bundle bundle = getIntent().getExtras();
if (bundle != null){
Glide.with(Update_Activity.this)
.load(bundle.getString("oldimageUrl"))
.into(update_Recipe_Image);
update_Recipe_Name.setText(bundle.getString("recipeNameKey"));
update_Recipe_Description.setText(bundle.getString("recipeDescriptionKey"));
update_Recipe_Price.setText(bundle.getString("recipePriceKey"));
}
}
}
LogCat中没有任何错误。
This is Detail Activity... I am sending data from this activity to next activity using putExtra in intent
public class Detail_Activity extends AppCompatActivity {
ImageView ivImage2;
TextView txtDescription, txtName, txtPrice;
String key = "";
String imageUrl = "";
Button delete_Recipe, update_Recipe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
ivImage2 = (ImageView)findViewById(R.id.ivImage2);
txtDescription = (TextView) findViewById(R.id.txtDescription);
txtName = (TextView) findViewById(R.id.txtName);
txtPrice = (TextView) findViewById(R.id.txtPrice);
delete_Recipe = (Button) findViewById(R.id.delete_Recipe);
update_Recipe = (Button) findViewById(R.id.update_Recipe);
update_Recipe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(Detail_Activity.this, Upload_Recipe.class)
.putExtra("recipeNameKey", txtName.getText().toString())
.putExtra("recipeDescriptionKey", txtDescription.getText().toString())
.putExtra("recipePriceKey", txtPrice.getText().toString())
.putExtra("oldimageUrl", imageUrl)
.putExtra("key",key)
);
}
});
**This is the Update Activity... In this activity, data is not showing or don't show the received data, while the code is correct **
public class Update_Activity extends AppCompatActivity {
ImageView update_Recipe_Image;
EditText update_Recipe_Name, update_Recipe_Description, update_Recipe_Price;
Button updatebutton;
Uri uri;
ActivityResultLauncher<String> mContent;
String imageUrl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update);
update_Recipe_Image = (ImageView) findViewById(R.id.update_Recipe_Image);
update_Recipe_Name = (EditText) findViewById(R.id.update_Recipe_Name);
update_Recipe_Description = (EditText) findViewById(R.id.update_Recipe_Description);
update_Recipe_Price = (EditText) findViewById(R.id.update_Recipe_Price);
updatebutton = (Button) findViewById(R.id.updatebutton);
Bundle bundle = getIntent().getExtras();
if (bundle != null){
Glide.with(Update_Activity.this)
.load(bundle.getString("oldimageUrl"))
.into(update_Recipe_Image);
update_Recipe_Name.setText(bundle.getString("recipeNameKey"));
update_Recipe_Description.setText(bundle.getString("recipeDescriptionKey"));
update_Recipe_Price.setText(bundle.getString("recipePriceKey"));
}
}
}
There is no any error in the logcat.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在将.putextra作为起始性参数之前,请尝试使用.putextra创建意图。如果要传递数据以更新活动,则需要将意向目的地赋予该活动的目的地:
Try creating the intent with the .putExtra before you put it as a parameter for startActivity. If you want to pass the data to update activity you need to give the destination of intent to that activity Like:
您不是在捆绑包中传递数据,而是使用
Intent
直接传递数据。因此,您可以在下一个屏幕上接收数据:只需用
bundle.getString
getIntent()。getStringExtra
update> update> update_activityYou are not passing data in bundle, you are directly passing data using
intent
. So you can receive your data in next screen like this:Just need to replace
bundle.getString
withgetIntent().getStringExtra
in yourUpdate_Activity
请注意这里有3个活动。 lidet_actiivty。 upload_recipe(是活动吗?),update_activity。您应该使用目标类创建意图。
Note there are 3 activities here. Detail_Actiivty. Upload_Recipe (is it activity?), Update_Activity. You should create Intent with the destination class.