如何将多个字符串传递给另一个活动?

时间:2016-03-09 03:30:06

标签: java android android-intent

我正在尝试制作一个测试Android应用程序。我正在尝试将用户出错的问题和多项选择显示在另一项活动中。我不知道如何转移多个错误的问题。

这是我的测验java类。

if Form1.focused = true then
   'process 
elseif form2.focused = true then
   'process
end if 

这是我的结果类

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类。

public class ResultActivityMarketing extends AppCompatActivity {

    //get text view for score
    TextView testResult = (TextView) findViewById(R.id.testResult);
    //get score
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        int score = extras.getInt("score");
        int numberquestions = extras.getInt("button");
        testResult.setText("You got " + score + " out of " + numberquestions + " questions correct");
    }
}

}

有人可以帮助我。我真的很感激。我不知道该怎么做。提前谢谢!

1 个答案:

答案 0 :(得分:5)

首先创建一个ArrayList类变量来保存失败的问题

private ArrayList<QuestionFialed> failedQuestions;

将每个失败的问题添加到QuestionFialed类的对象中,该类添加到failedQuestions arraylist中。

if (isAnswerWrong) {
        failedQuestions.add(new QuestionResult(questionNumber, selectedAnswer, correctAnswer));
    }

然后在测验结束时,您可以将包含失败问题的ArrayList发送到另一个活动

if (isGameOver) {            
     Intent intent = new Intent(this, QuizResultActivity.class);
        intent.putParcelableArrayListExtra("FailedResult", failedQuestions);
        startActivity(intent);
}

在下一个活动页面上,您可以像这样

返回您的ArrayList
ArrayList<QuestionResult> 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);
            }
        }
    });

  }

}

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];
    }
  };
}

对于结果活动,您将获得失败的问题计数并使用它来计算得分。

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