【问题标题】:Android Studio: Passing multiple values using intent PROVIDES ONLY 1 VALUE?Android Studio:使用意图传递多个值仅提供 1 个值?
【发布时间】:2016-04-11 05:46:52
【问题描述】:

我正在尝试做一个测验,让用户在每个活动中选择一个答案,最后一页会根据所选选项提供一个答案。因此,我想将这些值传递给最终活动,但似乎只显示最后选择的值。

第一个活动:

public class Quiz extends Activity {

Button btn;
RadioGroup rg;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quiz);

    btn = (Button) findViewById(R.id.nextBtn);
    rg= (RadioGroup) findViewById(R.id.rg);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {
            if (rg.getCheckedRadioButtonId() == -1) {

                Toast.makeText(getApplicationContext(), "Please select an answer",
                        Toast.LENGTH_SHORT).show();
            } else{
            Intent intent = new Intent(getApplicationContext(), Quiz1.class);
            Bundle bundle = new Bundle();
            int id = rg.getCheckedRadioButtonId();
            RadioButton radioButton = (RadioButton) findViewById(id);
            bundle.putString("rg", radioButton.getText().toString());
            intent.putExtras(bundle);
            startActivity(intent);

        }

        }

    });
  }
}

第二个活动:

public class Quiz1 extends Activity {

Button btn;
RadioGroup rg1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quiz1);

    btn = (Button) findViewById(R.id.nextBtn1);
    rg1= (RadioGroup) findViewById(R.id.rg1);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {
            if (rg1.getCheckedRadioButtonId() == -1) {

                Toast.makeText(getApplicationContext(), "Please select an answer",
                        Toast.LENGTH_SHORT).show();
            } else{
                Intent intent1 = new Intent(getApplicationContext(), Quiz2.class);
                Bundle bundle1 = new Bundle();
                int id = rg1.getCheckedRadioButtonId();
                RadioButton radioButton = (RadioButton) findViewById(id);
                bundle1.putString("rg1", radioButton.getText().toString());
                intent1.putExtras(bundle1);
                startActivity(intent1);

            }

        }

    });
  }
}

现在总共有 7 个活动.. 不包括最终活动 这是第 7 次(称为 Quiz6)活动:

public class Quiz6 extends Activity {

Button btn;
RadioGroup rg6;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quiz6);


    btn = (Button) findViewById(R.id.nextBtn6);
    rg6= (RadioGroup) findViewById(R.id.rg6);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {
            if (rg6.getCheckedRadioButtonId() == -1) {

                Toast.makeText(getApplicationContext(), "Please select an answer",
                        Toast.LENGTH_SHORT).show();
            } else{
                Intent intent6 = new Intent(getApplicationContext(), Final1.class);
                Bundle bundle6 = new Bundle();
                int id = rg6.getCheckedRadioButtonId();
                RadioButton radioButton = (RadioButton) findViewById(id);
                bundle6.putString("rg6", radioButton.getText().toString());
                intent6.putExtras(bundle6);
                startActivity(intent6);

            }

        }

    });
  }
}

你明白了:)

这里是最后的活动(称为 Final1),这里显示结果

这里是代码

public class Final1 extends Activity {

Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.final1);

    Bundle bundle = getIntent().getExtras();

    TextView textView = (TextView)findViewById(R.id.txt);
    textView.setText(bundle.getCharSequence("rg"));

    Bundle bundle1 = getIntent().getExtras();
    TextView textView1 = (TextView)findViewById(R.id.txt1);
    textView.setText(bundle1.getCharSequence("rg1"));

    Bundle bundle2 = getIntent().getExtras();
    TextView textView2 = (TextView)findViewById(R.id.txt2);
    textView.setText(bundle2.getCharSequence("rg2"));

    Bundle bundle3 = getIntent().getExtras();
    TextView textView3 = (TextView)findViewById(R.id.txt3);
    textView.setText(bundle3.getCharSequence("rg3"));

    Bundle bundle4 = getIntent().getExtras();
    TextView textView4 = (TextView)findViewById(R.id.txt4);
    textView.setText(bundle4.getCharSequence("rg4"));

    Bundle bundle5 = getIntent().getExtras();
    TextView textView5 = (TextView)findViewById(R.id.txt5);
    textView.setText(bundle5.getCharSequence("rg5"));

    Bundle bundle6 = getIntent().getExtras();
    TextView textView6 = (TextView)findViewById(R.id.txt6);
    textView.setText(bundle6.getCharSequence("rg6"));

    btn = (Button)findViewById(R.id.restartBtn);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent in = new Intent(v.getContext(), Quiz.class);
            startActivityForResult(in, 0);
        }
    });

  }

}

运行程序后最终发生的事情是,只有“textView”最终会在最终活动之前更改为活动上的所选选项,如上所示

感谢任何帮助,非常感谢

【问题讨论】:

    标签: java android-intent android-studio android-bundle


    【解决方案1】:

    一个活动的意图只有一个Bundle与之关联。因此,您只是将最后一个包传递给您的最终活动,其他包则传递给紧随其后的活动。您可以通过

    解决此问题
    Bundle bundle = getIntent().getExtras(); //this gets the current activity's extras
    RadioButton radioButton = (RadioButton) findViewById(id);
    bundle.putString("rg1", radioButton.getText().toString());
    intent.putExtras(bundle);
    startActivity(intent);
    

    而不是像现在这样每次都创建一个新包。然后将此捆绑包传递给以下活动。这样,每次您获得新答案时,您都将其添加到您的答案包中,而不是每次都创建一个只有一个项目的新包。

    然后在你的最后一个活动中,你只需要得到一个包,并且可以从中提取所有答案。注意:您需要确保使用正确的文本更新正确的文本视图。在您的问题中,textView 是唯一更新了 6 次的视图,而其他视图则根本没有更新。

    Bundle bundle = getIntent().getExtras();
    
    TextView textView = (TextView)findViewById(R.id.txt);
    textView.setText(bundle.getCharSequence("rg"));
    
    TextView textView1 = (TextView)findViewById(R.id.txt1);
    textView1.setText(bundle.getCharSequence("rg1"));
    
    TextView textView2 = (TextView)findViewById(R.id.txt2);
    textView2.setText(bundle.getCharSequence("rg2"));
    
    TextView textView3 = (TextView)findViewById(R.id.txt3);
    textView3.setText(bundle.getCharSequence("rg3"));
    
    TextView textView4 = (TextView)findViewById(R.id.txt4);
    textView4.setText(bundle.getCharSequence("rg4"));
    
    TextView textView5 = (TextView)findViewById(R.id.txt5);
    textView5.setText(bundle.getCharSequence("rg5"));
    
    TextView textView6 = (TextView)findViewById(R.id.txt6);
    textView6.setText(bundle.getCharSequence("rg6"));
    

    【讨论】:

    • 但是在我的第一个活动(测验)中,我不必先做Bundle bundle1 = new Bundle();吗?而且我实际上是否必须在每次活动之后专门通过并接收它,或者我可以在最后的活动中一次性完成吗?因为我不知道您在下一个活动中收到它是什么意思,所以我不确定如何准确地做到这一点:$ 抱歉,我是菜鸟。谢谢
    • 你是对的,在第一个活动中,你会想要一个新的Bundle。之后,您将获取传递给当前活动的包,为其添加一个值,并将相同的包传递给下一个活动。这样,当您结束时,捆绑包中包含您在活动中添加的所有内容。
    • 哦,我明白了 :) 但我是否还必须添加一个代码才能让它在下一个活动中接收它,或者它会通过 Bundle bundle = getIntent().getExtras(); 自动接收它,因为我还在只得到最后一个值
    • 这就是这种方法的好处。 Android 为您做到了! Activity 收到 Intent 时它会保存它,您可以随时使用getIntent() 访问它。
    • 所以现在我有} else{ Intent intent = new Intent(getApplicationContext(), Quiz6.class); // Bundle bundle5 = new Bundle(); Bundle bundle = getIntent().getExtras(); int id = rg5.getCheckedRadioButtonId(); RadioButton radioButton = (RadioButton) findViewById(id); bundle.putString("rg5", radioButton.getText().toString()); intent.putExtras(bundle); 并且在第一个活动中我有Bundle bundle = new Bundle(); 而不是getIntent,仍然是同样的问题:(
    猜你喜欢
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-11
    • 2020-01-05
    • 1970-01-01
    • 1970-01-01
    • 2020-12-25
    相关资源
    最近更新 更多