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
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 ?
Therefore you need to put your array list of into the bundle. For this you need to use the method putParcelableArrayList 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
You can pass the arraylist of your custom define object either by implementing serializable or parcelable. Android: Difference between Parcelable and Serializable? Show activity on this post. 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
Java ArrayList. The ArrayList class is a resizable array, which can be found in the java.util package. The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified (if you want to add or remove elements to/from an array, you have to create a new one). While elements can be added and removed ...
The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified (if you want to add or remove elements to/from an array, you have to create a new one). While elements can be added and removed from an ArrayList whenever you want. The syntax is also slightly different:
First, make InfoProduct
Parcelable
Check this Link
Then use
bundle.putParcelableArrayList(product)
You can get ArrayList
by
getParcelableArrayList(...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With