通过意图传递对象的 ArrayList - Java (Android) [英] Passing ArrayList of objects through intent - Java (Android)

查看:32
本文介绍了通过意图传递对象的 ArrayList - Java (Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过一个意图传递一个对象的 ArrayList,但无法让它工作.这是我所拥有的:

I am trying to pass an ArrayList of objects through an intent but cannot get it to work. Here is what I have:

public class QuestionView extends Activity {

    //variables and other methods...

    public void endQuiz() {
        Intent intent = new Intent(QuestionView.this, Results.class);
        intent.putExtra("correctAnswers", correctAnswers);
        intent.putExtra("wrongAnswers", wrongAnswers);
        intent.putParcelableArrayListExtra("queries", queries);
        startActivity(intent);
    }
}

这里正在接收意图:

public class Results extends Activity {

    int cAnswers;
    int wAnswers;
    ArrayList<Question> qs;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.resultsmain);

        cAnswers = getIntent().getIntExtra("correctAnswers", -1);
        wAnswers = getIntent().getIntExtra("wrongAnswers", -1);

        qs = getIntent().getParcelableArrayListExtra("queries");

                    //more code...
    }
}

正在接收两个整数,正确答案和错误答案,我可以使用它们.ArrayList 没有通过.endQuiz() 方法中没有错误,但 'qs = getIntent().getParcelableArrayListExtra("queries");'抛出错误并说绑定不匹配".

The two ints, correctAnswer and wrongAnswers, are being received and I can use them. The ArrrayList is not coming through. No errors on in the endQuiz() method but the 'qs = getIntent().getParcelableArrayListExtra("queries");' is throwing an error and saying "Bound Mismatch."

对此的任何帮助表示赞赏!

Any help on this is appreciated!

问题类:

public class Question {
    String a1;
    String a2;
    String a3;
    String a4;
    int correctAnswer;
    String query;
    int selectedAnswer;
    boolean correctness;

    public Question() {
    }

    public Question(String a1, String a2, String a3, String a4, int correctAnswer, String query, int selectedAnswer, boolean correctness) {
        this.a1 = a1;
        this.a2 = a2;
        this.a3 = a3;
        this.a4 = a4;
        this.correctAnswer = correctAnswer;
        this.query = query;
        this.selectedAnswer = selectedAnswer;
        this.correctness = correctness;
    }
    }

推荐答案

您必须更改您的 Question 类以实际实现 Parcelable.Parcelable 界面起初可能会令人困惑……但请坚持下去.

You must change your Question class to actually implement Parcelable. The Parcelable interface can be confusing at first... but hang in there.

您应该关注两种 Parcelable 方法:

There are two Parcelable methods that you should focus on:

  • writeToParcel() 将您的类转换为 Parcel 对象.
  • Question(Parcel in) 将 Parcel 对象转换回类的可用实例.
  • writeToParcel() which converts your class into a Parcel object.
  • Question(Parcel in) which converts a Parcel object back into usable instance of your class.

您可以安全地切割 &粘贴我标记的其他 Parcelable 信息.

You can safely cut & paste the other Parcelable information that I marked.

为简单起见,我将仅使用部分 Question 类:

For the sake of simplicity I will only use part of your Question class:

public class Question implements Parcelable {
    String a1;
    String a2;
    ...

    public Question(String a1, String a1) {
        this.a1 = a1;
        this.a2 = a2;
    }

    // Your existing methods go here. (There is no need for me to re-write them.) 

    // The following methods that are required for using Parcelable
    private Question(Parcel in) {
        // This order must match the order in writeToParcel()
        a1 = in.readString();
        a2 = in.readString();
        // Continue doing this for the rest of your member data
    }

    public void writeToParcel(Parcel out, int flags) {
        // Again this order must match the Question(Parcel) constructor
        out.writeString(a1);
        out.writeString(a2);
        // Again continue doing this for the rest of your member data
    }

    // Just cut and paste this for now
    public int describeContents() {
        return 0;
    }

    // Just cut and paste this for now
    public static final Parcelable.Creator<Question> CREATOR = new Parcelable.Creator<Question>() {
        public Question createFromParcel(Parcel in) {
            return new Question(in);
        }

        public Question[] newArray(int size) {
            return new Question[size];
        }
    };
}

现在更改将 queries 放入 Intent extra 的方式.

Now change how you put queries into your Intent extras.

intent.putParcelableArrayListExtra("queries", queries);

您读取 Parcelable 数组的方式是完美的.

The way you read the Parcelable array is perfect as it is.

这篇关于通过意图传递对象的 ArrayList - Java (Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆