如何使用Android Studio中的捆绑包将数据发送到下一个活动

发布于 2025-02-01 17:15:45 字数 2940 浏览 3 评论 0原文

这是详细活动...我正在使用意图中的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

铁轨上的流浪者 2025-02-08 17:15:45
startActivity(new Intent(Detail_Activity.this, Upload_Recipe.class)
                        .putExtra("recipeNameKey", txtName.getText().toString())

在将.putextra作为起始性参数之前,请尝试使用.putextra创建意图。如果要传递数据以更新活动,则需要将意向目的地赋予该活动的目的地:

Intent myIntent= new Intent(Detail_Activity.this, Update_Activity.class)
                        .putExtra("recipeNameKey", txtName.getText().toString())
startActivity(myIntent)
startActivity(new Intent(Detail_Activity.this, Upload_Recipe.class)
                        .putExtra("recipeNameKey", txtName.getText().toString())

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 myIntent= new Intent(Detail_Activity.this, Update_Activity.class)
                        .putExtra("recipeNameKey", txtName.getText().toString())
startActivity(myIntent)
谈情不如逗狗 2025-02-08 17:15:45

您不是在捆绑包中传递数据,而是使用Intent直接传递数据。因此,您可以在下一个屏幕上接收数据:

 if (getIntent() != null){
            Glide.with(Update_Activity.this)
                    .load(getIntent().getStringExtra("oldimageUrl"))
                    .into(update_Recipe_Image);
            update_Recipe_Name.setText(getIntent().getStringExtra("recipeNameKey"));
            update_Recipe_Description.setText(getIntent().getStringExtra("recipeDescriptionKey"));
            update_Recipe_Price.setText(getIntent().getStringExtra("recipePriceKey"));

        }

只需用bundle.getString getIntent()。getStringExtra update> update> update_activity

You are not passing data in bundle, you are directly passing data using intent. So you can receive your data in next screen like this:

 if (getIntent() != null){
            Glide.with(Update_Activity.this)
                    .load(getIntent().getStringExtra("oldimageUrl"))
                    .into(update_Recipe_Image);
            update_Recipe_Name.setText(getIntent().getStringExtra("recipeNameKey"));
            update_Recipe_Description.setText(getIntent().getStringExtra("recipeDescriptionKey"));
            update_Recipe_Price.setText(getIntent().getStringExtra("recipePriceKey"));

        }

Just need to replace bundle.getString with getIntent().getStringExtra in your Update_Activity

孤凫 2025-02-08 17:15:45

请注意这里有3个活动。 lidet_actiivty。 upload_recipe(是活动吗?),update_activity。您应该使用目标类创建意图。

Intent intent = new Intent ( DetailActivity.this, Update_Activity.class) ....

Note there are 3 activities here. Detail_Actiivty. Upload_Recipe (is it activity?), Update_Activity. You should create Intent with the destination class.

Intent intent = new Intent ( DetailActivity.this, Update_Activity.class) ....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文