Android 使用 Bundle 传递自定义数据列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23441299/
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
Passing custom List of data using Bundle
提问by xenuit
I'm developing a simple app, that contains tabview with fragment. I am stuck at the place, where i have to pass data to my newly created fragment on tabselect.
我正在开发一个简单的应用程序,其中包含带有片段的 tabview。我被困在这个地方,我必须将数据传递给我在 tabselect 上新创建的片段。
I have a List of lists of my custom class objects:
我有一个自定义类对象的列表列表:
List<List<NewsObjectClass>> myList;
Here is where I got stuck:
这是我卡住的地方:
public static class PlaceholderFragment extends ListFragment{
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment(){
}
public static PlaceholderFragment newInstance(int sectionNumber, List<List<NewsObjectsClass>> data) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
// Here i want to pass my List<List<NewsObjectClass>> to the bundle
fragment.setArguments(args);
return fragment;
}
...
So specifically i need a way how to pass my list of lsits of myCustomObjects to the fragment, so there I could use it for lsitview adapter.
所以特别是我需要一种方法如何将 myCustomObjects 的 lsits 列表传递给片段,这样我就可以将它用于 lsitview 适配器。
Any syggestions on how to pass this type of data would be great. Thanks.
关于如何传递此类数据的任何 syggestions 都会很棒。谢谢。
回答by Alireza Sobhani
args.putParcelableArrayList(DATA_KEY, new ArrayList<>(data));
回答by Alexander Kulyakhtin
Make your NewObjectClassParcelableor Serializableand then create a new class, effectively, containing your list, also Parcelableor Serializable. Then use Bundle.putSerializable(or putParcelable)
创建您的NewObjectClassParcelableorSerializable然后创建一个新类,有效地包含您的列表,还有Parcelableor Serializable。然后使用Bundle.putSerializable(或putParcelable)
Or, simpler, make NewObjectClassParcelablethen use putParcelableArrayListif you can do with ArrayListinstead of generic List
或者,更简单,如果可以使用而不是通用,NewObjectClassParcelable则使用然后使用putParcelableArrayListArrayListList
Or, simplest, make NewObjectClassSerializable and use putSerializablepassing ArrayList<NewObjectClass>because ArrayListis Serializable
或者,最简单的,使NewObjectClassSerializable 并使用putSerializable传递,ArrayList<NewObjectClass>因为ArrayList是Serializable
In the last case perhaps you only will have to ad implementsSerializableto your class.
在最后一种情况下,您可能只需要implementsSerializable为您的班级做广告。
Alternatively, if your data seem to be large, consider keeping them in a custom Application-derived object instead. You extend Applicationand then such object will exist all the time your app exist. Don't forget to register it in manifest.
或者,如果您的数据看起来很大,请考虑将它们保存在自定义Application派生对象中。你扩展Application,然后这样的对象将一直存在你的应用程序存在。不要忘记在清单中注册它。
class MyApplication extends Application {
public static Object myData;
}
Or you can do with shared preferences
或者您可以使用共享首选项
PreferenceManager.getDefaultSharedPreferences().edit().putInt("a", 1).commit();
PreferenceManager.getDefaultSharedPreferences().getInt("a");
回答by Konstantin Konopko
use putSerializablemethod to pass your custom list.
使用putSerializable方法传递您的自定义列表。
args.putSerializable(KEY, ArrayList<Type>);
and fetch it using getSerializable
并使用它获取它 getSerializable
ArrayList<Type> list = (ArrayList<Type>) getArguments().getSerializable(KEY);
回答by Taha
Or, simplest, make NewObjectClass Serializable and use putSerializable passing ArrayList because ArrayList is Serializable
或者,最简单的方法是使 NewObjectClass Serializable 并使用 putSerializable 传递 ArrayList 因为 ArrayList 是 Serializable
Unfortunately, this did not work in my case. I ended up converting my array list to string (or JSON string) and send by
不幸的是,这在我的情况下不起作用。我最终将我的数组列表转换为字符串(或 JSON 字符串)并通过
bundle.putString()
and then, in fragment, parsed the string back to array list.
然后,在片段中,将字符串解析回数组列表。

