如何传递对象ArrayList中从一个在Android的使用意图的另一项活动?意图、对象、ArrayList、Android

2023-09-12 06:42:31 作者:抚媚不是妖

我有我的的onClick()方法按照code为

 名单,其中;问题> mQuestionsList = QuestionBank.getQuestions();
 

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

 意图resultIntent =新的意图(这一点,ResultActivity.class);
  resultIntent.putParcelableArrayListExtra(QuestionsExtra,(ArrayList中和LT ;?延伸Parcelable>)mQuestionsList);
  startActivity(resultIntent);
 

我不知道如何从一个活动传递了这个问题列出了意向另一个活动 我的问题类

 公共类问题{
    私人INT []操作数;
    私人INT []选择;
    私人诠释userAnswerIndex;

    公众质疑(INT []操作数,INT []选择){
        this.operands =操作数;
        this.choices =选择;
        this.userAnswerIndex = -1;
    }

    公众诠释[] getChoices(){
        返回的选择;
    }

    公共无效setChoices(INT []选择){
        this.choices =选择;
    }

    公众诠释[] getOperands(){
        返回操作数;
    }

    公共无效setOperands(INT []操作数){
        this.operands =操作数;
    }

    公众诠释getUserAnswerIndex(){
        返回userAnswerIndex;
    }

    公共无效setUserAnswerIndex(INT userAnswerIndex){
        this.userAnswerIndex = userAnswerIndex;
    }

    公众诠释的getAnswer(){
        INT答案= 0;
        对于(INT操作数:操作数){
            答案+ =操作;
        }
        返回的答案;
    }

    公共布尔isCorrect(){
        返回的getAnswer()==选择[this.userAnswerIndex];
    }

    公共布尔hasAnswered(){
        返回userAnswerIndex!= -1;
    }

    @覆盖
    公共字符串的toString(){
        StringBuilder的建设者=新的StringBuilder();

        // 题
        builder.append(问:);
        对于(INT操作数:操作数){
            builder.append(的String.Format(%D操作数));
        }
        builder.append(System.getProperty(line.separator));

        //选择
        INT答案=的getAnswer();
        对于(INT选择:选择){
            如果(选择==回答){
                builder.append(的String.Format(%D(A),选择));
            } 其他 {
                builder.append(的String.Format(%D,选择));
            }
        }
        返回builder.toString();
       }

      }
 
Android官方架构组件指南

解决方案

它运作良好,

 公共类问题实现Serializable {
    私人INT []操作数;
    私人INT []选择;
    私人诠释userAnswerIndex;

   公众质疑(INT []操作数,INT []选择){
       this.operands =操作数;
       this.choices =选择;
       this.userAnswerIndex = -1;
   }

   公众诠释[] getChoices(){
       返回的选择;
   }

   公共无效setChoices(INT []选择){
       this.choices =选择;
   }

   公众诠释[] getOperands(){
       返回操作数;
   }

   公共无效setOperands(INT []操作数){
       this.operands =操作数;
   }

   公众诠释getUserAnswerIndex(){
       返回userAnswerIndex;
   }

   公共无效setUserAnswerIndex(INT userAnswerIndex){
       this.userAnswerIndex = userAnswerIndex;
   }

   公众诠释的getAnswer(){
       INT答案= 0;
       对于(INT操作数:操作数){
           答案+ =操作;
       }
       返回的答案;
   }

   公共布尔isCorrect(){
       返回的getAnswer()==选择[this.userAnswerIndex];
   }

   公共布尔hasAnswered(){
       返回userAnswerIndex!= -1;
   }

   @覆盖
   公共字符串的toString(){
       StringBuilder的建设者=新的StringBuilder();

       // 题
       builder.append(问:);
       对于(INT操作数:操作数){
           builder.append(的String.Format(%D操作数));
       }
       builder.append(System.getProperty(line.separator));

       //选择
       INT答案=的getAnswer();
       对于(INT选择:选择){
           如果(选择==回答){
               builder.append(的String.Format(%D(A),选择));
           } 其他 {
               builder.append(的String.Format(%D,选择));
           }
       }
       返回builder.toString();
     }
  }
 

在源活动,用这个:

 名单,其中;问题> mQuestionList =新的ArrayList<问题取代;
  mQuestionsList = QuestionBank.getQuestions();
  mQuestionList.add(新课题(OPS1,choices1));

  意向意图=新的意图(SourceActivity.this,TargetActivity.class);
  intent.putExtra(QuestionListExtra,ArrayList的<问题> mQuestionList);
 

在你的目标的活动,用这个:

 名单,其中;问题>问题=新的ArrayList<问题>();
  问题=(ArrayList的<问题>)getIntent()getSerializableExtra(QuestionListExtra)。
 

I have the following in code in my onClick() method as

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

Now I have the intent after this line, as follows :

  Intent resultIntent = new Intent(this, ResultActivity.class);
  resultIntent.putParcelableArrayListExtra("QuestionsExtra", (ArrayList<? extends Parcelable>) mQuestionsList);
  startActivity(resultIntent);

I don't know how to pass this question lists in the intent from one activity to another activity My Question class

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

    @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();
       }

      }

解决方案

It works well,

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

In your Source Activity, use this :

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

In your Target Activity, use this :

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