Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put the ArrayList into bundle

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 ?

like image 832
Jeff Liu Avatar asked Feb 24 '17 10:02

Jeff Liu


People also ask

How to put ArrayList in bundle in Android?

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

How to pass an ArrayList to a custom define object?

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

What is ArrayList in Java?

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 ...

What is the difference between a built-in array and an ArrayList?

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:


1 Answers

First, make InfoProduct Parcelable Check this Link

Then use

bundle.putParcelableArrayList(product)

You can get ArrayList by

getParcelableArrayList(...)
like image 111
Mohit Kacha Avatar answered Oct 27 '22 00:10

Mohit Kacha