简体   繁体   English

通过intent传递对象的ArrayList - Java(Android)

[英]Passing ArrayList of objects through intent - Java (Android)

I am trying to pass an ArrayList of objects through an intent but cannot get it to work. 我试图通过意图传递对象的ArrayList但无法让它工作。 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);
    }
}

Intents are being received here: 意在这里收到:

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

The two ints, correctAnswer and wrongAnswers, are being received and I can use them. 正在接收两个整数,correctAnswer和wrongAnswers,我可以使用它们。 The ArrrayList is not coming through. ArrrayList没有通过。 No errors on in the endQuiz() method but the 'qs = getIntent().getParcelableArrayListExtra("queries");' 在endQuiz()方法中没有错误,但'qs = getIntent()。getParcelableArrayListExtra(“queries”);' is throwing an error and saying "Bound Mismatch." 抛出错误并说“绑定不匹配”。

Any help on this is appreciated! 对此有任何帮助表示赞赏!

Question class: 问题类:

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

You must change your Question class to actually implement Parcelable. 您必须更改Question类以实际实现Parcelable。 The Parcelable interface can be confusing at first... but hang in there. Parcelable界面起初可能会让人感到困惑......但请挂在那里。

There are two Parcelable methods that you should focus on: 您应该关注两种Parcelable方法:

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

You can safely cut & paste the other Parcelable information that I marked. 您可以安全地剪切和粘贴我标记的其他Parcelable信息。

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

Now change how you put queries into your Intent extras. 现在更改将queries放入Intent附加组件的方式。

intent.putParcelableArrayListExtra("queries", queries);

The way you read the Parcelable array is perfect as it is. 您阅读Parcelable阵列的方式非常完美。

In order to be able to deliver an ArrayList as part of an Intent, your type must implement Parcelable. 为了能够将ArrayList作为Intent的一部分提供,您的类型必须实现Parcelable。 Judging by the fact you had to cast your List to (ArrayList<? extends Parcelable>) , you didn't do this. 从你必须将List转换为(ArrayList<? extends Parcelable>)的事实来判断,你没有这样做。 If you had, you could simply have: 如果你有,你可以简单地:

class Foo implements Parcelable {
//implementation here
}
ArrayList<Foo> foos = new ArrayList<Foo>();
new Intent().putParcelableArrayListExtra("foos", foos); //no need to cast

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM