猿问

如何在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;

    }


倚天杖
浏览 1193回答 3
3回答

慕桂英3389331

活动之间:为我工作ArrayList<Object> object = new ArrayList<Object>();Intent intent = new Intent(Current.class, Transfer.class);Bundle args = new Bundle();args.putSerializable("ARRAYLIST",(Serializable)object);intent.putExtra("BUNDLE",args);startActivity(intent);在Transfer.class中Intent intent = getIntent();Bundle args = intent.getBundleExtra("BUNDLE");ArrayList<Object> object = (ArrayList<Object>) args.getSerializable("ARRAYLIST");希望这个帮助的人。使用Parcelable在Activity之间传递数据创建DataModel后,通常可以使用例如,假设我们有一个json类型{&nbsp; &nbsp; "bird": [{&nbsp; &nbsp; &nbsp; &nbsp; "id": 1,&nbsp; &nbsp; &nbsp; &nbsp; "name": "Chicken"&nbsp; &nbsp; }, {&nbsp; &nbsp; &nbsp; &nbsp; "id": 2,&nbsp; &nbsp; &nbsp; &nbsp; "name": "Eagle"&nbsp; &nbsp; }]}这里的鸟是一个列表,它包含两个元素,因此我们将使用jsonschema2pojo创建模型现在我们有了包含Bird of List的模型类Name BirdModel和Bird BirdModel,并且Bird包含名称和ID转到bird类并添加接口“ 实现Parcelable ”通过Alt + Enter在android studio中添加implemets方法注意:将出现一个对话框,提示添加工具方法,然后按Enter通过按Alt + Enter添加Parcelable实现注意:将出现一个对话框,显示“添加可打包实施并再次输入”现在将其传递给意图。List<Bird> birds = birdModel.getBird();Intent intent = new Intent(Current.this, Transfer.class);Bundle bundle = new Bundle();bundle.putParcelableArrayList("Birds", birds);intent.putExtras(bundle);startActivity(intent);并在转移活动onCreateList<Bird> challenge = this.getIntent().getExtras().getParcelableArrayList("Birds");谢谢如果有任何问题,请告诉我。

犯罪嫌疑人X

脚步:将您的对象类实现为可序列化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");

弑天下

效果很好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

相关分类

Android
我要回答