Android Studio:通过多个活动传递多个值只显示一个? :(

时间:2016-01-07 06:20:02

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

你好:)所以这里是我的“应用程序”应该如何工作。有多个活动,每个活动中都有一个问题,其答案可以通过radiobuttons选择。它最终会进入显示结果的最终活动。

这是第一个活动的代码

`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 intent = new Intent(getApplicationContext(), Quiz2.class);
                Bundle bundle = getIntent().getExtras();
                int id = rg1.getCheckedRadioButtonId();
                RadioButton radioButton = (RadioButton) findViewById(id);
                bundle.putString("rg1", radioButton.getText().toString());
                intent.putExtras(bundle);
                startActivity(intent);

            }

        }

    });
  }
}

第三名:

public class Quiz2 extends Activity {

    Button btn;
    RadioGroup rg2;


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


        btn = (Button) findViewById(R.id.nextBtn2);
        rg2= (RadioGroup) findViewById(R.id.rg2);

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

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

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

                }

            }

        });
      }
    }

这种趋势继续进行7项活动

以下是最终代码:

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

    TextView textView1 = (TextView)findViewById(R.id.txt1);
    textView.setText(bundle.getCharSequence("rg1"));

    TextView textView2 = (TextView)findViewById(R.id.txt2);
    textView.setText(bundle.getCharSequence("rg2"));

    TextView textView3 = (TextView)findViewById(R.id.txt3);
    textView.setText(bundle.getCharSequence("rg3"));

    TextView textView4 = (TextView)findViewById(R.id.txt4);
    textView.setText(bundle.getCharSequence("rg4"));

    TextView textView5 = (TextView)findViewById(R.id.txt5);
    textView.setText(bundle.getCharSequence("rg5"));

    TextView textView6 = (TextView)findViewById(R.id.txt6);
    textView.setText(bundle.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);
        }
    });

  }
}

这是我运行应用程序后在最后一页中得到的内容, textViews显示文本设置为在FINAL结果页面之前选择的选项... 其余的textViews(所以textView1,2,3,4,5,6)都是空的

感谢任何帮助,谢谢<3

2 个答案:

答案 0 :(得分:0)

对于您的情况,保存和检索数据的最佳方法是使用Sharedpreferences.

在所有活动中,将共享偏好初始化为

SharedPreferences prefs = this.getSharedPreferences("myprefs", Context.MODE_PRIVATE);

而不是保存在捆绑中,另存为

prefs.edit.putString("rg2", radioButton.getText().toString()).apply();

然后,在您的最终活动中检索为

prefs.getString("rg2","");

如果要使用Intents进行此操作,则必须将旧数据传递到每个活动中的新活动。 所以,在你的第二节课中,

intent.putExtras("rg",bundle.getString("rg")); );
intent.putExtras("rg1", radioButton.getText().toString());

依旧......

答案 1 :(得分:0)

改为使用共享偏好

制作以下课程并在需要时使用。

    public class PreferenceManager {
    private static final String PREFERENCES_QUIZ_ANS = "quiz_ans";
    private static final String QUIZ_ANS_1 = "quiz_ans_1";

    private static SharedPreferences getQuizAnsPreferences(Context context) {
        return context.getSharedPreferences(PREFERENCES_QUIZ_ANS, Activity.MODE_PRIVATE);
    }

    public static void storeQuizAns_1(Context context, String quiz_ans_1){
        getServicePreferences(context).edit().putString(QUIZ_ANS_1, quiz_ans_1).commit();
    }

    public static String getQuizAns_1(Context context){
        return getServicePreferences(context).getString(QUIZ_ANS_1, null);
    }
}

在每个活动中使用如下所示来存储答案。

PreferenceManager.storeQuizAns_1(getApplicationContext(), radioButton.getText().toString());

在FINAL活动中使用如下所示获取答案并打印到 TEXTVIEW

TextView textView1 = (TextView)findViewById(R.id.txt1); textView1.setText(PreferenceManager.getQuizAns_1(this));

你必须像问题一样制作像storeQuizAns_1和getQuizAns_1这样的方法。 你必须像QUIZ_ANS_1这样的常量和问题一样。