Jeff Liu
Jeff Liu

Reputation: 141

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 ?

Upvotes: 8

Views: 25914

Answers (4)

Mohit Kacha
Mohit Kacha

Reputation: 504

First, make InfoProduct Parcelable Check this Link

Then use

bundle.putParcelableArrayList(product)

You can get ArrayList by

getParcelableArrayList(...)

Upvotes: 15

jason.kaisersmith
jason.kaisersmith

Reputation: 9620

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

Therefore you need to put your array list of into the bundle. For this you need to use the method 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

This post might also help you with that.

Upvotes: 1

RoShan Shan
RoShan Shan

Reputation: 2954

You can pass the arraylist of your custom define object either by implementing serializable or parcelable.

Android: Difference between Parcelable and Serializable?

Upvotes: 2

nipun.birla
nipun.birla

Reputation: 719

You need to use bundle.putParcelableArrayList() and instantiate your list as private ArrayList<InfoProduct> product;.Also make sure that InfoProduct is Parcelable.

Upvotes: 2

Related Questions