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

13

我正在尝试通过意图(Intent)传递一个对象的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);
    }
}

这里正在接收Intents:

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

这两个int类型的变量correctAnswer和wrongAnswers已经接收并可以使用。但是ArrayList没有传递过来,虽然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
你的 Question 类实现了 Parcelable 接口吗? - wsanville
3
“不……这就是他要把它转换的原因…… (ArrayList<? extends Parcelable>) queries); :),这肯定不会有帮助。” - Selvin
我们能看到你的查询 ArrayList 的类型实际上是什么吗?我的意思是类... - LuxuryMode
@Selvin,但他没有收到ClassCastException异常,这很奇怪... - LuxuryMode
1
Java泛型...类似的代码在C#中无法编译,但在Java中可以...ArrayList<Object> a = null; ArrayList<? extends Parcelable> b = (ArrayList<? extends Parcelable>) a;可以工作...只有警告Unchecked cast from ArrayList<Object> to ArrayList<? extends Parcelable>...在Java中,在运行时你不知道泛型类型。 - Selvin
显示剩余2条评论
2个回答

27

你必须修改你的Question类以实现Parcelable接口。Parcelable接口可能会一开始让人感到困惑,但是要坚持下去。

有两个Parcelable方法是你需要重点关注的:

  • writeToParcel() 把你的类转换成一个Parcel对象。
  • Question(Parcel in) 把一个Parcel对象转换成可用的类实例。

可以安全地复制粘贴我标记的其他Parcelable信息。

为了简单起见,我只会使用你的Question类的一部分:

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 附加项的方法。
intent.putParcelableArrayListExtra("queries", queries);

您读取Parcelable数组的方式已经非常完美,无需更改。


谢谢你详细的回答,Sam!我没有把所有的代码都给你,所以可能让你走了弯路。我编辑了我的开头帖子,并加入了所有的类。现在,我应该在QuestionView或Question中实现Parcelable并添加所有这些其他方法呢? - Matt
你只想传递一个问题的ArrayList,因此只有Question类应该实现Parcelable。 - Sam
哦,太棒了,它运行起来了!这是我在这个项目上已经走过的最远的路程,已经有两个月了。今天是一个重要的里程碑!再次感谢你,Sam。 :) - Matt
@Sam 怎么打包一个位图? - Darshan Miskin

7

为了能够将ArrayList作为Intent的一部分进行传递,您的类型必须实现Parcelable接口。从您需要将List强制转换为(ArrayList<? extends Parcelable>)来看,您似乎没有这样做。如果您这样做了,您只需要:

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

不一定需要实现Parcelable,但是使用Serializable会导致性能损失,因此你应该使用Parcelable。 - Ajay Takur

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接