Java 如何将多个字符串传递给另一个活动?
我正在尝试做一个Android应用程序测试。我试图显示用户在其他活动中出错的问题和多项选择。我不知道如何转移多个错误的问题 这是我的问答java类Java 如何将多个字符串传递给另一个活动?,java,android,android-intent,Java,Android,Android Intent,我正在尝试做一个Android应用程序测试。我试图显示用户在其他活动中出错的问题和多项选择。我不知道如何转移多个错误的问题 这是我的问答java类 public class QuizActivityMarketing extends Activity{ private Iterator<Question> questionIterator; private TextView txtQuestion; private RadioButton rda, rdb, rdc, rdd; pu
public class QuizActivityMarketing extends Activity{
private Iterator<Question> questionIterator;
private TextView txtQuestion;
private RadioButton rda, rdb, rdc, rdd;
public Button butNext;
private int qid = 0;
private Question currentQ;
private int score = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
// Define your views
txtQuestion = (TextView) findViewById(R.id.textView1);
rda = (RadioButton) findViewById(R.id.radio0);
rdb = (RadioButton) findViewById(R.id.radio1);
rdc = (RadioButton) findViewById(R.id.radio2);
rdd = (RadioButton) findViewById(R.id.radio3);
butNext = (Button) findViewById(R.id.button1);
//Log question and option if wrong
TextView question = (TextView) findViewById(R.id.textView1);
RadioButton optA = (RadioButton) findViewById(R.id.radio0);
RadioButton optB = (RadioButton) findViewById(R.id.radio1);
RadioButton optC = (RadioButton) findViewById(R.id.radio2);
RadioButton optD = (RadioButton) findViewById(R.id.radio3);
if (!currentQ.getANSWER().trim().equals(answer.getText())) {
Log.d("Current Question", ""+question.getText());
Log.d("OptionA", ""+optA.getText());
Log.d("OptionB", ""+optB.getText());
Log.d("OptionC", "" + optC.getText());
Log.d("OptionD", "" + optD.getText());
}
// Load the next question, if there are any
if (questionIterator.hasNext()) {
currentQ = questionIterator.next();
setQuestionView(currentQ);
qid++;
}
// Done asking questions
Intent intent1 = getIntent();
int buttonValue = Integer.valueOf(intent1.getExtras().getString("button"));
if (qid > buttonValue)
{
这是我的结果活动的XML类
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/scrollView" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/wrong_questions_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Wrong Questions"
android:paddingTop="10dp"/>
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.04" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:layout_marginTop="30dp"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radio3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RadioButton"/>
</RadioGroup>
</FrameLayout>
</ScrollView>
</LinearLayout>
谁能帮帮我吗。我会非常感激的。我不知道怎么做。提前谢谢 首先创建一个ArrayList类变量,用于保存失败的问题
private ArrayList<QuestionFialed> failedQuestions;
然后在测验结束时,您可以将保存失败问题的ArrayList发送到另一个活动
if (isGameOver) {
Intent intent = new Intent(this, QuizResultActivity.class);
intent.putParcelableArrayListExtra("FailedResult", failedQuestions);
startActivity(intent);
}
在下一个活动页面上,您可以像下面这样返回您的ArrayList
ArrayList<QuestionResult> myFailedQuestions = getIntent().getParcelableArrayListExtra("FailedResult");
ArrayList myFailedQuestions=getIntent().getParcelableArrayListExtra(“FailedResult”);
我希望这将为你指明正确的方向
根据您的具体需要进行更新
我看不到你的全部代码,但这是如何将其应用到你的应用程序中
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import java.util.ArrayList;
public class QuizActivityMarketing extends AppCompatActivity {
private Iterator<Question> questionIterator;
private TextView txtQuestion;
private RadioButton rda, rdb, rdc, rdd;
private RadioGroup radioGroup;
public Button butNext;
private int qid = 0;
private Question currentQ;
private int score = 0;
// store failed questions and correct answer
private ArrayList<QuestionFailed> failedQuestions;
// holds currently selected user answer
private String currentSelectedAnswer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz_activity_marketing);
// create object of a ArrayList and clear the ArrayList object
failedQuestions = new ArrayList<QuestionFailed>();
failedQuestions.clear();
// Define your views
txtQuestion = (TextView) findViewById(R.id.textView1);
rda = (RadioButton) findViewById(R.id.radio0);
rdb = (RadioButton) findViewById(R.id.radio1);
rdc = (RadioButton) findViewById(R.id.radio2);
rdd = (RadioButton) findViewById(R.id.radio3);
// RadioGroup
// select the current RadioButton selected
radioGroup = (RadioGroup)findViewById(R.id.radioGroup1);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == R.id.radio0){
currentSelectedAnswer = (RadioButton)((RadioButton) findViewById(R.id.radio0)).getText().toString();
}
else if(checkedId == R.id.radio1){
currentSelectedAnswer = (RadioButton)((RadioButton) findViewById(R.id.radio1)).getText().toString();
}
else if(checkedId == R.id.radio2){
currentSelectedAnswer = (RadioButton)((RadioButton) findViewById(R.id.radio2)).getText().toString();
}
else if(checkedId == R.id.radio3){
currentSelectedAnswer = (RadioButton)((RadioButton) findViewById(R.id.radio3)).getText().toString();
}
else{
currentSelectedAnswer = "";
}
}
});
// use button event to move to next question if there is more
butNext = (Button) findViewById(R.id.button1);
butNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// check if the user selected result matches correct answer or not
if(currentSelectedAnswer.equals(currentQ.getANSWER().trim())){
// answer is correct
}else{
//incorrect answer selected
// then add failed answer in the QuestionFailed object inside ArrayList
failedQuestions.add(new QuestionResult(qid, currentSelectedAnswer, currentQ.getANSWER()));
}
// check if there is more questions
if (questionIterator.hasNext()) {
currentQ = questionIterator.next();
//Display next questions
setQuestionView(currentQ);
//increment question number
qid++;
}
else{
// There is no question left which means that the quiz is over
// send the failed answer to next activity
Intent intent = new Intent(this, ResultActivityMarketing.class);
intent.putParcelableArrayListExtra("FailedResult", failedQuestions);
startActivity(intent);
}
}
});
}
}
导入android.content.Intent;
导入android.os.Bundle;
导入android.support.v7.app.AppActivity;
导入android.util.Log;
导入android.view.view;
导入android.widget.Button;
导入android.widget.RadioButton;
导入android.widget.RadioGroup;
导入android.widget.TextView;
导入java.util.ArrayList;
公共类QuizaActivityMarketing扩展了AppCompativity活动{
私有迭代器;
私有文本视图TXT问题;
专用单选按钮rda、rdb、rdc、rdd;
私人放射组放射组;
公共按钮butNext;
私有int-qid=0;
私人问题;
私人智力得分=0;
//存储失败的问题和正确答案
私人ArrayList失败问题;
//保存当前选定的用户答案
私有字符串currentSelectedAnswer;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u quick\u activity\u marketing);
//创建ArrayList的对象并清除ArrayList对象
failedQuestions=新建ArrayList();
失败的问题;
//定义您的视图
txtQuestion=(TextView)findViewById(R.id.textView1);
rda=(RadioButton)findViewById(R.id.radio0);
rdb=(RadioButton)findViewById(R.id.radio1);
rdc=(RadioButton)findViewById(R.id.radio2);
rdd=(RadioButton)findViewById(R.id.radio3);
//放射组
//选择当前选中的单选按钮
放射组=(放射组)findViewById(R.id.radioGroup 1);
radioGroup.setOnCheckedChangeListener(新的radioGroup.OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(RadioGroup组,int checkedId){
if(checkedId==R.id.0){
currentSelectedAnswer=(RadioButton)((RadioButton)findViewById(R.id.radio0)).getText().toString();
}
else if(checkedId==R.id.radio1){
currentSelectedAnswer=(RadioButton)((RadioButton)findViewById(R.id.radio1)).getText().toString();
}
else if(checkedId==R.id.radio2){
currentSelectedAnswer=(RadioButton)((RadioButton)findViewById(R.id.radio2)).getText().toString();
}
else if(checkedId==R.id.radio3){
currentSelectedAnswer=(RadioButton)((RadioButton)findViewById(R.id.radio3)).getText().toString();
}
否则{
currentSelectedAnswer=“”;
}
}
});
//如果还有更多问题,请使用按钮事件转到下一个问题
但是下一步=(按钮)findViewById(R.id.button1);
但是next.setOnClickListener(newview.OnClickListener(){
@凌驾
公共void onClick(视图v){
//检查用户选择的结果是否与正确答案匹配
如果(currentSelectedAnswer.equals(currentQ.getANSWER().trim())){
//答案是正确的
}否则{
//选择了不正确的答案
//然后在ArrayList中的QuestionFailed对象中添加失败的答案
失败的问题。添加(新问题结果(qid,currentSelectedAnswer,currentQ.getANSWER());
}
//检查是否还有其他问题
if(questionIterator.hasNext()){
currentQ=questionIterator.next();
//显示下一个问题
设置问题视图(currentQ);
//递增问题编号
qid++;
}
否则{
//没有问题了,这意味着测验结束了
//将失败的答案发送到下一个活动
Intent Intent=新的Intent(这是ResultActivityMarketing.class);
意图。putParcelableArrayListExtra(“失败的结果”,失败的问题);
星触觉(意向);
}
}
});
}
}
QuestionFailed类实现了Parcelable。见全班同学
import android.os.Parcel;
import android.os.Parcelable;
public class QuestionFailed implements Parcelable {
private int questionId;
private String yourAnswer;
private String correctAnswer;
public QuestionFailed(int questionId, String yourAnswer, String correctAnswer) {
this.questionId = questionId;
this.yourAnswer = yourAnswer;
this.correctAnswer = correctAnswer;
}
public QuestionFailed(Parcel in) {
questionId = in.readInt();
yourAnswer = in.readString();
correctAnswer = in.readString();
}
public int getQuestionId() {
return questionId;
}
public String getYourAnswer() {
return yourAnswer;
}
public String getCorrectAnswer() {
return correctAnswer;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(questionId);
dest.writeString(yourAnswer);
dest.writeString(correctAnswer);
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator<QuestionFailed>() {
public QuestionFailed createFromParcel(Parcel in) {
return new QuestionFailed(in);
}
public QuestionFailed[] newArray(int size) {
return new QuestionFailed[size];
}
};
}
导入android.os.packet;
导入android.os.Parcelable;
公共类问题无法实现可包裹{
私人智力测验;
你的答案是私人字符串;
私人字符串更正应答;
public QuestionFailed(int questionId,String yourAnswer,String correctAnswer){
this.questionId=questionId;
this.yourAnswer=你的答案;
this.correctAnswer=correctAnswer;
}
公开质询失败(包裹在中){
questionId=in.readInt();
yourAnswer=in.readString();
correctAnswer=in.readString();
}
public int getQuestionId(){
返回问题ID;
}
公共字符串getYourAnswer(){
回答你的问题;
}
公共字符串getCorrectAnswer(){
返回正确答案;
}
@凌驾
公共int描述内容(){
返回0;
}
@凌驾
公共无效写入包裹(包裹目的地,内部标志){
目的写入(问题ID);
目的地令状
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import java.util.ArrayList;
public class QuizActivityMarketing extends AppCompatActivity {
private Iterator<Question> questionIterator;
private TextView txtQuestion;
private RadioButton rda, rdb, rdc, rdd;
private RadioGroup radioGroup;
public Button butNext;
private int qid = 0;
private Question currentQ;
private int score = 0;
// store failed questions and correct answer
private ArrayList<QuestionFailed> failedQuestions;
// holds currently selected user answer
private String currentSelectedAnswer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz_activity_marketing);
// create object of a ArrayList and clear the ArrayList object
failedQuestions = new ArrayList<QuestionFailed>();
failedQuestions.clear();
// Define your views
txtQuestion = (TextView) findViewById(R.id.textView1);
rda = (RadioButton) findViewById(R.id.radio0);
rdb = (RadioButton) findViewById(R.id.radio1);
rdc = (RadioButton) findViewById(R.id.radio2);
rdd = (RadioButton) findViewById(R.id.radio3);
// RadioGroup
// select the current RadioButton selected
radioGroup = (RadioGroup)findViewById(R.id.radioGroup1);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == R.id.radio0){
currentSelectedAnswer = (RadioButton)((RadioButton) findViewById(R.id.radio0)).getText().toString();
}
else if(checkedId == R.id.radio1){
currentSelectedAnswer = (RadioButton)((RadioButton) findViewById(R.id.radio1)).getText().toString();
}
else if(checkedId == R.id.radio2){
currentSelectedAnswer = (RadioButton)((RadioButton) findViewById(R.id.radio2)).getText().toString();
}
else if(checkedId == R.id.radio3){
currentSelectedAnswer = (RadioButton)((RadioButton) findViewById(R.id.radio3)).getText().toString();
}
else{
currentSelectedAnswer = "";
}
}
});
// use button event to move to next question if there is more
butNext = (Button) findViewById(R.id.button1);
butNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// check if the user selected result matches correct answer or not
if(currentSelectedAnswer.equals(currentQ.getANSWER().trim())){
// answer is correct
}else{
//incorrect answer selected
// then add failed answer in the QuestionFailed object inside ArrayList
failedQuestions.add(new QuestionResult(qid, currentSelectedAnswer, currentQ.getANSWER()));
}
// check if there is more questions
if (questionIterator.hasNext()) {
currentQ = questionIterator.next();
//Display next questions
setQuestionView(currentQ);
//increment question number
qid++;
}
else{
// There is no question left which means that the quiz is over
// send the failed answer to next activity
Intent intent = new Intent(this, ResultActivityMarketing.class);
intent.putParcelableArrayListExtra("FailedResult", failedQuestions);
startActivity(intent);
}
}
});
}
}
import android.os.Parcel;
import android.os.Parcelable;
public class QuestionFailed implements Parcelable {
private int questionId;
private String yourAnswer;
private String correctAnswer;
public QuestionFailed(int questionId, String yourAnswer, String correctAnswer) {
this.questionId = questionId;
this.yourAnswer = yourAnswer;
this.correctAnswer = correctAnswer;
}
public QuestionFailed(Parcel in) {
questionId = in.readInt();
yourAnswer = in.readString();
correctAnswer = in.readString();
}
public int getQuestionId() {
return questionId;
}
public String getYourAnswer() {
return yourAnswer;
}
public String getCorrectAnswer() {
return correctAnswer;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(questionId);
dest.writeString(yourAnswer);
dest.writeString(correctAnswer);
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator<QuestionFailed>() {
public QuestionFailed createFromParcel(Parcel in) {
return new QuestionFailed(in);
}
public QuestionFailed[] newArray(int size) {
return new QuestionFailed[size];
}
};
}
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.ArrayList;
public class ResultActivityMarketing extends AppCompatActivity {
public static final int totalQuizCount = 30;
private ArrayList<QuestionFailed> myFailedQuestions;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result_activity_marketing);
myFailedQuestions = getIntent().getParcelableArrayListExtra("FailedResult");
int failedCount = myFailedQuestions.size();
// get the score
int score = 30 - failedCount;
// Display the result
TextView testResult = (TextView) findViewById(R.id.testResult);
testResult.setText("You got " + String.valueOf(score) + " out of " + String.valueOf(numberquestions) + " questions correct");
}
}