为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕田峪7331174 2019-11-03 12:04:21
我的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;    }
查看完整描述

3 回答

?
墨色风雨

TA贡献1853条经验 获得超6个赞

脚步:


将您的对象类实现为可序列化


public class Question implements Serializable`

将此放到您的源活动中


ArrayList<Question> mQuestionList = new ArrayList<Question>;

mQuestionsList = QuestionBank.getQuestions();  

mQuestionList.add(new Question(ops1, choices1));


Intent intent = new Intent(SourceActivity.this, TargetActivity.class);

intent.putExtra("QuestionListExtra", mQuestionList);

将此放入您的目标活动中


 ArrayList<Question> questions = new ArrayList<Question>();

 questions = (ArrayList<Questions>) getIntent().getSerializableExtra("QuestionListExtra");



查看完整回答
反对 回复 2019-11-04
?
Qyouu

TA贡献1786条经验 获得超11个赞

效果很好


public class Question implements Serializable {

    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;

   }


   @Override

   public String toString() {

       StringBuilder builder = new StringBuilder();


       // Question

       builder.append("Question: ");

       for(int operand : operands) {

           builder.append(String.format("%d ", operand));

       }

       builder.append(System.getProperty("line.separator"));


       // Choices

       int answer = getAnswer();

       for (int choice : choices) {

           if (choice == answer) {

               builder.append(String.format("%d (A) ", choice));

           } else {

               builder.append(String.format("%d ", choice));

           }

       }

       return builder.toString();

     }

  }

在您的“源活动”中,使用以下命令:


  List<Question> mQuestionList = new ArrayList<Question>;

  mQuestionsList = QuestionBank.getQuestions();

  mQuestionList.add(new Question(ops1, choices1));


  Intent intent = new Intent(SourceActivity.this, TargetActivity.class);

  intent.putExtra("QuestionListExtra", ArrayList<Question>mQuestionList);

在您的目标活动中,使用以下命令:


  List<Question> questions = new ArrayList<Question>();

  questions = (ArrayList<Question>)getIntent().getSerializableExtra("QuestionListExtra");



查看完整回答
反对 回复 2019-11-04
  • 3 回答
  • 0 关注
  • 326 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信