我使用 bundle 将项目的 ArrayList 从一个 Activity 传递到另一个 Activity 。我没有收到任何错误,但项目没有显示在我的第二个 Activity 中。我是否正确实现了 getSerialized() 和 putSerialized() ?
这是我的第一个 Activity 的 fragment
声明ListArray
ArrayList<Item> items = new ArrayList<Item>();
将元素放入 bundle 的位置
Intent ListIntent = new Intent(home.this, SectionListExampleActivity.class);
Bundle loadInfo = new Bundle();
loadInfo.putSerializable("items", items);
ListIntent.putExtras(loadInfo);
第二个 Activity
Bundle loadInfo = getIntent().getExtras();
items = (ArrayList<Item>) loadInfo.getSerializable("items");
我在这两项 Activity 中都实现了 Serialized。我已确保 ArrayList 确实在第一个 Activity 中填充
最佳答案
只是猜测:您是否尝试过序列化 Item 数组 (Item[]) 而不是 ArrayList。我不确定 ArrayList 是否可序列化。
loadInfo.putSerializable("items", items.toArray());
Bundle loadInfo = getIntent().getExtras();
items = (Item[]) loadInfo.getSerializable("items");
关于java - Android 获取序列化(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10100705/