请问如何在Android中使用Intent将对象的ArrayList从一个传递到另一个活动?

我的onClick()方法中的代码如下


 List<Question> mQuestionsList = QuestionBank.getQuestions();

现在,我有这行之后的意图,如下所示:


  Intent resultIntent = new Intent(this, ResultActivity.class);

  resultIntent.putParcelableArrayListExtra("QuestionsExtra", (ArrayList<? extends Parcelable>) mQuestionsList);

  startActivity(resultIntent);

我不知道如何在意图中将此问题列表从一个活动传递到另一个活动


public class Question {

    private int[] operands;

    private int[] choices;

    private int userAnswerIndex;


    public Question(int[] operands, int[] choices) {

        this.operands = operands;

        this.choices = choices;

        this.userAnswerIndex = -1;

    }


    public int[] getChoices() {

        return choices;

    }


    public void setChoices(int[] choices) {

        this.choices = choices;

    }


    public int[] getOperands() {

        return operands;

    }


    public void setOperands(int[] operands) {

        this.operands = operands;

    }


    public int getUserAnswerIndex() {

        return userAnswerIndex;

    }


    public void setUserAnswerIndex(int userAnswerIndex) {

        this.userAnswerIndex = userAnswerIndex;

    }


    public int getAnswer() {

        int answer = 0;

        for (int operand : operands) {

            answer += operand;

        }

        return answer;

    }


    public boolean isCorrect() {

        return getAnswer() == choices[this.userAnswerIndex];

    }


    public boolean hasAnswered() {

        return userAnswerIndex != -1;

    }


慕田峪7331174
浏览 327回答 3
3回答

墨色风雨

脚步:将您的对象类实现为可序列化public class Question implements Serializable`将此放到您的源活动中ArrayList<Question> mQuestionList = new ArrayList<Question>;mQuestionsList = QuestionBank.getQuestions();&nbsp;&nbsp;mQuestionList.add(new Question(ops1, choices1));Intent intent = new Intent(SourceActivity.this, TargetActivity.class);intent.putExtra("QuestionListExtra", mQuestionList);将此放入您的目标活动中&nbsp;ArrayList<Question> questions = new ArrayList<Question>();&nbsp;questions = (ArrayList<Questions>) getIntent().getSerializableExtra("QuestionListExtra");

Qyouu

效果很好public class Question implements Serializable {&nbsp; &nbsp; private int[] operands;&nbsp; &nbsp; private int[] choices;&nbsp; &nbsp; private int userAnswerIndex;&nbsp; &nbsp;public Question(int[] operands, int[] choices) {&nbsp; &nbsp; &nbsp; &nbsp;this.operands = operands;&nbsp; &nbsp; &nbsp; &nbsp;this.choices = choices;&nbsp; &nbsp; &nbsp; &nbsp;this.userAnswerIndex = -1;&nbsp; &nbsp;}&nbsp; &nbsp;public int[] getChoices() {&nbsp; &nbsp; &nbsp; &nbsp;return choices;&nbsp; &nbsp;}&nbsp; &nbsp;public void setChoices(int[] choices) {&nbsp; &nbsp; &nbsp; &nbsp;this.choices = choices;&nbsp; &nbsp;}&nbsp; &nbsp;public int[] getOperands() {&nbsp; &nbsp; &nbsp; &nbsp;return operands;&nbsp; &nbsp;}&nbsp; &nbsp;public void setOperands(int[] operands) {&nbsp; &nbsp; &nbsp; &nbsp;this.operands = operands;&nbsp; &nbsp;}&nbsp; &nbsp;public int getUserAnswerIndex() {&nbsp; &nbsp; &nbsp; &nbsp;return userAnswerIndex;&nbsp; &nbsp;}&nbsp; &nbsp;public void setUserAnswerIndex(int userAnswerIndex) {&nbsp; &nbsp; &nbsp; &nbsp;this.userAnswerIndex = userAnswerIndex;&nbsp; &nbsp;}&nbsp; &nbsp;public int getAnswer() {&nbsp; &nbsp; &nbsp; &nbsp;int answer = 0;&nbsp; &nbsp; &nbsp; &nbsp;for (int operand : operands) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;answer += operand;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;return answer;&nbsp; &nbsp;}&nbsp; &nbsp;public boolean isCorrect() {&nbsp; &nbsp; &nbsp; &nbsp;return getAnswer() == choices[this.userAnswerIndex];&nbsp; &nbsp;}&nbsp; &nbsp;public boolean hasAnswered() {&nbsp; &nbsp; &nbsp; &nbsp;return userAnswerIndex != -1;&nbsp; &nbsp;}&nbsp; &nbsp;@Override&nbsp; &nbsp;public String toString() {&nbsp; &nbsp; &nbsp; &nbsp;StringBuilder builder = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp;// Question&nbsp; &nbsp; &nbsp; &nbsp;builder.append("Question: ");&nbsp; &nbsp; &nbsp; &nbsp;for(int operand : operands) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;builder.append(String.format("%d ", operand));&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;builder.append(System.getProperty("line.separator"));&nbsp; &nbsp; &nbsp; &nbsp;// Choices&nbsp; &nbsp; &nbsp; &nbsp;int answer = getAnswer();&nbsp; &nbsp; &nbsp; &nbsp;for (int choice : choices) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (choice == answer) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;builder.append(String.format("%d (A) ", choice));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;builder.append(String.format("%d ", choice));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;return builder.toString();&nbsp; &nbsp; &nbsp;}&nbsp; }在您的“源活动”中,使用以下命令:&nbsp; List<Question> mQuestionList = new ArrayList<Question>;&nbsp; mQuestionsList = QuestionBank.getQuestions();&nbsp; mQuestionList.add(new Question(ops1, choices1));&nbsp; Intent intent = new Intent(SourceActivity.this, TargetActivity.class);&nbsp; intent.putExtra("QuestionListExtra", ArrayList<Question>mQuestionList);在您的目标活动中,使用以下命令:&nbsp; List<Question> questions = new ArrayList<Question>();&nbsp; questions = (ArrayList<Question>)getIntent().getSerializableExtra("QuestionListExtra");
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android