java Android Studio:通过多个活动传递多个值只显示一个?:(
你好:)下面是我的“应用程序”的工作原理。有多个活动,每个活动中都有一个问题,可以用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);
}
});
}
}
这是我运行应用程序后在最后一页中看到的内容, 文本视图在最终结果页之前显示设置为所选选项的文本。。。 其余的文本视图(所以文本视图1、2、3、4、5、6)都是空的
感谢您的帮助<;三,
# 1 楼答案
改为使用共享引用
在课堂下制作并在需要时使用
在每个活动中都像下面一样使用它来存储答案
在最后的活动中,如下图所示使用它获取答案并打印到文本视图中
TextView textView1 = (TextView)findViewById(R.id.txt1); textView1.setText(PreferenceManager.getQuizAns_1(this));
您必须尽可能多地使用storeQuizAns_1和getQuizAns_1等方法。 你必须尽可能多地制造像测验1这样的常量
# 2 楼答案
对于您的情况,保存和检索数据的最佳方法是使用
Sharedpreferences.
在所有活动中,将SharedReferences初始化为
而不是保存在bundle中,而是另存为
然后,在最后的活动中,检索为
如果您想使用意图进行此操作,则必须将旧数据传递给每个活动中的新活动。 所以,在你的第二节课上
等等