java 如何将 ArrayList 放入 bundle
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42436012/
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
How to put the ArrayList into bundle
提问by Jeff Liu
I have put some value into arraylist like this :
我已经将一些值放入数组列表中,如下所示:
private List<InfoProduct> product;
product = new ArrayList<>();
product.add(new InfoProduct(R.drawable.cloth, Product_title, "0", Product_data2, lunch,R.drawable.cardview_circle_corner));
product.add(new InfoProduct(R.drawable.guitar, Product_title, "1", Product_data2, lunch,R.drawable.cardview_circle_corner));
product.add(new InfoProduct(R.drawable.cloth, Product_title, "2", Product_data2, lunch,R.drawable.cardview_circle_corner));
product.add(new InfoProduct(R.drawable.guitar, Product_title, "3", Product_data2, lunch,R.drawable.cardview_circle_corner));
product.add(new InfoProduct(R.drawable.cloth, Product_title, "4", Product_data2, lunch,R.drawable.cardview_circle_corner));
product.add(new InfoProduct(R.drawable.guitar, Product_title, "5", Product_data2, lunch,R.drawable.cardview_circle_corner));
product.add(new InfoProduct(R.drawable.cloth, Product_title, "6", Product_data2, lunch,R.drawable.cardview_circle_corner));
product.add(new InfoProduct(R.drawable.guitar, Product_title, "7", Product_data2, lunch,R.drawable.cardview_circle_corner));
product.add(new InfoProduct(R.drawable.cloth, Product_title, "8", Product_data2, lunch,R.drawable.cardview_circle_corner));
product.add(new InfoProduct(R.drawable.guitar, Product_title, "9", Product_data2, lunch,R.drawable.cardview_circle_corner));
when I need to go other Fragment , I want to use Bundle to transfer my ArrayList , so I write some code to comply this
当我需要去其他 Fragment 时,我想使用 Bundle 来传输我的 ArrayList ,所以我写了一些代码来遵守这个
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_youtube, new FragmentYoutubeProduct(), "youtube");
ft.addToBackStack("youtube");
Bundle bundle = new Bundle();
bundle.putStringArrayList("Product",product);
ft.commit();
I got error on bundle.putStringArrayList("Product",product);
, how can I do to resolve this question ?
我出错了bundle.putStringArrayList("Product",product);
,我该如何解决这个问题?
回答by Mohit Kacha
First make InfoProduct Parceble Check this- Link
首先制作InfoProduct Parceble Check this- Link
Then use `
然后使用`
bundle.putParcelableArrayList(product)
You can get arraylist by `
你可以通过`获取arraylist
getParcelableArrayList(..)
Also check this
也检查这个
回答by nipun.birla
You need to use bundle.putParcelableArrayList()
and instantiate your list as private ArrayList<InfoProduct> product;
.Also make sure that InfoProduct is Parcelable.
您需要使用bundle.putParcelableArrayList()
并将您的列表实例化为 private ArrayList<InfoProduct> product;
。还要确保 InfoProduct 是 Parcelable。
回答by RoShan Shan
You can pass the arraylist of your custom define object either by implementing serializable
or parcelable
.
您可以通过实现serializable
或传递自定义定义对象的数组列表parcelable
。
回答by jason.kaisersmith
Your code of
你的代码
bundle.putStringArrayList("Product",product);
Is trying to place an array list of strings into the bundle. However, you don't have an array list of strings. You have an array list of InfoProduct
正在尝试将字符串数组列表放入包中。但是,您没有字符串数组列表。你有一个数组列表InfoProduct
Therefore you need to put your array list of into the bundle. For this you need to use the methodputParcelableArrayList
因此,您需要将您的数组列表放入包中。为此,您需要使用该方法putParcelableArrayList
bundle.putParcelableArrayList("Product", product);
In order to do this you also need to ensure that your object InfoProduct
in parcelable, which is serializable by Android. So you need to have it implement the Parcelable interface
为了做到这一点,您还需要确保您的对象可InfoProduct
打包,Android 可对其进行序列化。所以你需要让它实现Parcelable 接口
This postmight also help you with that.
这篇文章也可能对你有所帮助。