通过意图传递对象的数组列表 - Java (Android)

2022-09-04 06:16:54

我正在尝试通过意图传递对象的 ArrayList,但无法使其正常工作。这是我所拥有的:

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

两个整数,correctAnswer和wrightAnswers,正在被接收,我可以使用它们。ArrrayList没有通过。在 endQuiz() 方法中没有错误,但 'qs = getIntent().getParcelableArrayListExtra(“queries”);' 抛出一个错误并说“Bound Mismatch”。

任何这方面的帮助是值得赞赏的!

问题类:

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

答案 1

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

您应该关注两种可 Parcelable 方法:

  • writeToParcel()这会将您的类转换为 Parcel 对象。
  • Question(Parcel in)这会将 Parcel 对象转换回类的可用实例。

您可以安全地剪切并粘贴我标记的其他可包裹信息。

为了简单起见,我将只使用您的问题类的一部分:

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

现在,更改您放入 Intent 附加内容的方式。queries

intent.putParcelableArrayListExtra("queries", queries);

您阅读可 Parcelable 数组的方式是完美的。


答案 2

为了能够将 ArrayList 作为 Intent 的一部分提供,您的类型必须实现 Parcelable。从你必须把你的列表扔到的事实来看,你没有这样做。如果有,您可以简单地拥有:(ArrayList<? extends Parcelable>)

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

推荐