java 安卓 getSerializable()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10100705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:37:37  来源:igfitidea点击:

Android getSerializable()

javaandroidserialization

提问by James Fazio

I am passing an ArrayList of Items from one activity to another using a bundle. I am not getting any errors, but the Items do not display in my second activity. Am I implementing the getSerializable() and putSerializable() correctly?

我正在使用一个包将一个 ArrayList 的项目从一个活动传递到另一个活动。我没有收到任何错误,但项目没有显示在我的第二个活动中。我是否正确实现了 getSerializable() 和 putSerializable()?

Here is a snippet from my first Activity

这是我的第一个活动的片段

ListArray Declared

ListArray 声明

ArrayList<Item> items = new ArrayList<Item>();

Where the items are put into a bundle

将物品放入捆绑包的位置

 Intent ListIntent = new Intent(home.this, SectionListExampleActivity.class);
 Bundle loadInfo = new Bundle();
 loadInfo.putSerializable("items", items);
 ListIntent.putExtras(loadInfo);

Second Activity

第二个活动

Bundle loadInfo = getIntent().getExtras();
    items = (ArrayList<Item>) loadInfo.getSerializable("items");

I have implemented Serializable in both activities. I have ensured that the ArrayList does get populated in the first activity

我在这两个活动中都实现了 Serializable。我确保在第一个活动中填充了 ArrayList

回答by ngesh

What is Item..? is it Serializable... if not make Serializable

什么是Item..?它是可序列化的……如果不是的话Serializable

回答by Bharat Godhani

Item class :

物品类别:

public class Item implements Serializable

In first Activity :

在第一个活动中:

Intent intent = new Intent(this, Activity2.class);
        intent.putExtra("items", items);
        startActivity(intent);

In Second Activity (Activity2):

在第二个活动 (Activity2) 中:

ArrayList<Item> items = (ArrayList<Item>) getIntent().getExtras()
                .getSerializable("items");

回答by k3b

Just a guess: have you tried to serialize an array of Item (Item[]) instead of ArrayList. I am not shure if ArrayList is serializable.

只是猜测:您是否尝试过序列化 Item (Item[]) 而不是 ArrayList 的数组。我不确定 ArrayList 是否可序列化。

loadInfo.putSerializable("items", items.toArray());

Bundle loadInfo = getIntent().getExtras();
items = (Item[]) loadInfo.getSerializable("items");