Android Studio: Passing multiple values using intent PROVIDES ONLY 1 VALUE?

狂风中的少年 提交于 2019-12-22 00:13:00

问题


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.

First Activity:

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

        }

        }

    });
  }
}

Second Activity:

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

            }

        }

    });
  }
}

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

            }

        }

    });
  }
}

You get the idea :)

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

here is the 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);
        }
    });

  }

}

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

Any help is appreciated, thanks lots <3


回答1:


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.

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


来源:https://stackoverflow.com/questions/34644942/android-studio-passing-multiple-values-using-intent-provides-only-1-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!