Android Studio:使用意图传递多个值仅提供 1 个值? [英] Android Studio: Passing multiple values using intent PROVIDES ONLY 1 VALUE?

查看:28
本文介绍了Android Studio:使用意图传递多个值仅提供 1 个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to make a quiz where users choose an answer in each activity, and the final page provides an answer based on the chosen options. Therefore, I want to pass these values to the final activity, but only the last chosen value seems to show.

第一个活动:

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 个活动.. 不包括最终活动这是 7ths(称为 Quiz6)活动:

Now this follows for a total of 7 activities.. not including the final activity Here is the 7ths (called Quiz6) activity:

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);

            }

        }

    });
  }
}

你明白了:)

这里是 FINAL 活动(称为 Final1),结果显示

Here is the FINAL activity (called Final1) here the results are show

这是代码

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"会在最终活动之前更改为活动中的所选选项,如上所示

What ends up happening once I run the program is that only "textView" ends up changing to the chosen choice on the activity right before the final activity, shown above

感谢任何帮助,非常感谢 <3

Any help is appreciated, thanks lots <3

推荐答案

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

An activity's intent only has one Bundle associated with it. Thus, you are only passing the last bundle to your final activity, the others are passed to the immediately following activity instead. You can fix this issue by doing

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);

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

instead of creating a new bundle each time as you do now. then pass this bundle along to the following activity. That way, each time you get a new answer, you are adding it to your bundle of answers, rather than creating a new bundle with only one item each time.

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

Then in your final activity, you only have to get the one bundle, and can pull all the answers out of it. NOTE: You'll want to make sure that your'e updating the correct textview with the correct text. In your question, textView is the only view that gets updated, 6 times, and the others do not get updated at all.

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"));

这篇关于Android Studio:使用意图传递多个值仅提供 1 个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆