当前位置: 首页 > 知识库问答 >
问题:

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

荆亦
2023-03-14

我的onclick()方法中有以下代码作为

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

现在我在这一行后面有了意图,如下所示:

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

我不知道如何把这个问题列表从一个activity的意图传递给另一个activity我的问题课

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

      }

共有2个答案

南门建章
2023-03-14

步骤:

>

  • 对象类实现为可序列化

    public class Question implements Serializable`
    

    把这个放进你的来源activity

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

    把这个放进你的目标activity

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

  • 张光辉
    2023-03-14

    activity:为我工作

    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后起作用

    例如,假设我们有一个类型为

    {
        "bird": [{
            "id": 1,
            "name": "Chicken"
        }, {
            "id": 2,
            "name": "Eagle"
        }]
    }
    

    这里,bird是一个列表,它包含两个元素,所以

    我们将使用jsonschema2pojo创建模型

    现在我们有了模型类Name BirdModel和Bird BirdModel由Bird列表组成,Bird包含Name和id

    转到bird类并添加接口“Implements Parcelable”

    通过Alt+Enter在android studio中添加implemets方法

    注意:会出现一个对话框,上面写着“添加实现方法”按Enter键

    按Alt+Enter的add Parcelable实现

    注意:会出现一个对话框,上面写着Add Parcelable implementation并再次输入

    现在把它传给意图。

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

    和转移activity onCreate

    List<Bird> challenge = this.getIntent().getExtras().getParcelableArrayList("Birds");
    

    谢谢

    如果有什么问题请告诉我。

     类似资料:
    • 问题内容: 我正在尝试从一个发送客户类的对象,Activity然后在另一个对象中显示它Activity。 客户类的代码: 我想将其对象从一个对象发送Activity到另一个对象,然后在另一个对象上显示数据Activity。 我该如何实现? 问题答案: 一种选择是让你的自定义类实现该接口,然后可以使用该方法的变体在意图中额外传递对象实例。 伪代码:

    • 我是java新手,我正试图编写我的第一个“更大”的程序,包含几个类。我在类“CompetitionProgram”中创建了一个ArrayList并将其声明为private。 我的程序中需要这两个类(Event&ListOfEvents),这是需求之一。事件--它只是表示事件本身,比如事件名称和尝试次数(它是一个体育事件)。和ListOfEvents--它表示列表本身,并包含用于添加事件、删除事件和

    • 我目前有一个ArrayList,其中包含“Product”对象,每个对象中包含一个字符串、整数和double。我希望能够使用参数搜索对象,找出与搜索参数匹配的ID(整数值),并将此对象复制到另一个ArrayList中。有没有可能通过迭代器实现这一点,或者有没有更简单的方法?

    • 问题内容: 我有两个 newAccessLevels.java ,它有两个按钮“ Level 1”,“ Level 2”和 newAccessPanel.java。 我需要获取用户选择“ 1或2”的级别,以便可以在标题中显示它。 accessPanel.java, 例如访问级别1,访问级别2。如何完成此操作。下面是示例代码,因此,如果单击级别1,则将打开标题为* ACCESS LEVEL 1 的n

    • 问题内容: 我想将多个值从一个servlet传递到另一个servlet。请告诉我如何通过? 问题答案: 取决于您是否使用会话: 使用session.setAttribute()将数组存储在会话变量中。 使用session.getAttribute();检索数组。 但是,变量将一直保留到会话终止,您用其他东西覆盖它或将其删除为止。 如果将一个servlet转发到另一个servlet,则可以将其存储在

    • 我的视图文件夹中有两个EJ,我创建了非常简单的EJ,看看是否可以将变量从一个EJ发送到另一个EJ。 a、 vews文件中的ejs b.ejs有 在我的节点js这是什么我做const表达式=要求('Express'); const body Parser=要求('body-parser'); 我想邮报必须在这里做点什么...