能不能通过一个ArrayList< Parcelable>到活动LT、ArrayList、GT、Parcelable

2023-09-12 04:47:41 作者:男人不花心゛除非来月

这是在code

ArrayList<MyObject> list = new ArrayList<MyObject>();
list.add(new MyObject());
Intent intent = new Intent(this, ReceiverActivity.class);
intent.putExtra("list", list);
startActivity(intent);

ReceiverActivity

ReceiverActivity

List<MyObject> list = (List<MyObject>)getIntent().getExtras().getParcelable("list");

下面列表为空。另外这个不工作:

Here list is null. Also this doesn't work:

List<MyObject> list = (List<MyObject>)getIntent().getExtras().getSerializable("list");

MyObject的是Parcelable,我实现了所有需要的方法。我想这个实现是没有问题的,因为否则我会recive其他类型的异常。但我不明白,除了列表中任何事情都是空。

MyObject is Parcelable, I implemented all required methods. I guess this implementation is not the problem, because otherwise I would recive other kind of exceptions. But I don't get anything besides list is null.

在此先感谢...

现在我发现这一点:

List<Parcelable> list = (List<Parcelable>)getIntent().getParcelableArrayListExtra("list");

这是在接收活动中使用,但我怎么把它和我如何获得名单,其中,为MyObject&GT; 名单,其中, Parcelable&GT;

that has to be used in the receiver activity, but how do I send it and how do I get List<MyObject> from List<Parcelable> ?

推荐答案

使用 i.putParcelableArrayListExtra(名称,值)其中i是你的意图。不使用putExtra()的parcelable的ArrayList

USe i.putParcelableArrayListExtra(name, value) where i is your intent. Dont use putExtra() for a parcelable ArrayList.