Android的工作室:传递多个值使用意图只提供了1 VALUE? [英] Android Studio: Passing multiple values using intent PROVIDES ONLY 1 VALUE?

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

问题描述

我试图做一个测验,用户选择每个活动的答案,并在最后一页提供了基于所选择的选项的答案。因此,我希望将这些值传递给最终的活动,但只有最后一次选择的值似乎显示。

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活动..不包括最后一个活动
这里是七和弦(称为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);

            }

        }

    });
  }
}

您明白了吧:)

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

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

这里是code

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

任何帮助是AP preciated,由于大量3;

Any help is appreciated, thanks lots <3

推荐答案

这是活动的意图只有一个捆绑与它相关联。因此,你只有经过最后一个包你最后的活动,其他被传递到紧随其后的活动来代替。您可以通过执行修复此问题。

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。在你的问题,的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的工作室:传递多个值使用意图只提供了1 VALUE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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