每次单击图像时,我都会使用不同的图像进行相对查看,我希望将唯一的文本发送到另一个活动。我已经能够为第一个图像实现此目的,但是我无法获得第二个图像来将文本发送到下一个活动。
我的首次活动代码:

        b1 = (ImageView) findViewById(R.id.native_amala);
        b2 = (ImageView) findViewById(R.id.native_fufu);
        b3 = (ImageView) findViewById(R.id.native_jollof);
        b4 = (ImageView) findViewById(R.id.native_ofada);
        b5 = (ImageView) findViewById(R.id.native_porridge);


        b1.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view){

                TextView foodName1 = (TextView) findViewById(R.id.amala_text_view);
                Intent intent = new Intent(NativeFoods.this,AddToCart.class);
                intent.putExtra("Amala",foodName1.getText().toString());
                startActivity(intent);
            }

        });
        b2.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view){

                TextView foodName1 = (TextView) findViewById(R.id.fufu_text_view);
                Intent intent2 = new Intent(NativeFoods.this,AddToCart.class);
                intent2.putExtra("fufu",foodName1.getText().toString());
                startActivity(intent2);
            }

        });

    }


我的第二项活动:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_to_cart);

        foodView = (TextView) findViewById(R.id.cart_item_text_view);
        Intent intent = getIntent();
        String str = intent.getStringExtra("Amala");
        foodView.setText(str);

最佳答案

您基本上需要了解putExtra的工作原理。

您可以使用键来输入一些值,例如

intent.putExtra("Amala",foodName1.getText().toString());


在这里,Amala是键,而foodName1.getText().toString()是值。

然后,在下一个活动中,您可以从键中检索值。

即您可以做到,

String str = intent.getStringExtra("Amala");


b2 ImageView的单击侦听器中,您提到键fufu,但是在第二个活动中,您将得到Amala

您需要为所有单击侦听器保留公用密钥,然后在第二个活动中从该密钥检索数据。

07-24 09:45
查看更多