1

I am using viewpager and creating fragments and i want to pass the arraylist. So i have tried the below things:

MainActivity:

 private ArrayList<customers> mArrayList = null;

  ViewPagerAdapter adapter = new ViewPagerAdapter(MainActivity.this.getSupportFragmentManager());

  adapter.addFrag(NewCustomer.newInstance(mArrayList ), "NewCustomer");

Now in fragment class i am creating instance:

 public static final ArrayList<customers> data = new ArrayList<customers>();

 public static final NewCustomer newInstance(ArrayList<customers> mArrayList) {

        NewCustomer f = new NewCustomer();
        Bundle bdl = new Bundle(1);
        bdl.putParcelableArrayList(data, mArrayList);
        f.setArguments(bdl);
        return f;
    }

But this is not working. It is showing error on bdl.putParcelableArrayList I want to get arraylist and store into the local arraylist of that class.

How can i get the arraylist with my custom object ?

3 Answers3

4

The first parameter you are passing is wrong. Check the definition:

putParcelableArrayList(String key, ArrayList<? extends Parcelable> value)

Define the key as following in your fragment:

public static final String KEY;

To get the arraylist as local variable in your fragment use following code:

@Override
public void onStart() {
    super.onStart();
    Bundle arguments = getArguments();
    ArrayList<customers> customer_list = arguments.getParcelable(KEY);
}
Hristo Stoyanov
  • 1,960
  • 1
  • 12
  • 24
  • That's well written. You could add the [source](https://developer.android.com/reference/android/os/Bundle.html) and also show how to call putParcelable with the KEY (that you need to initialize since this is a constant) – AxelH Dec 29 '16 at 12:22
0

Can you try ?

public static final NewCustomer newInstance(ArrayList<customers> mArrayList) {

        NewCustomer f = new NewCustomer();
        Bundle bdl = new Bundle(1);
        this.data = mArrayList; // assign its Value
        bdl.putParcelableArrayList(data, mArrayList);
        f.setArguments(bdl);
        return f;
    }

this.data = mArrayList will assign value to the current member variable of fragment. Now it can we be accessed in current fragment.

Mayur Raval
  • 3,250
  • 6
  • 34
  • 57
  • 1
    You should explain why the code you provide is working and what is it doing. If you provide simply a code, this won't help the OP and the following users that could end up with a similar question. – AxelH Dec 29 '16 at 12:10
  • Don't apologize ;) But how are you explaining the usage of `this` in a static context ? I don't really get that part. Or the wrong usage of the method ;) – AxelH Dec 29 '16 at 12:15
0

please check that your model class "NewCustomer" implements Parcelable.

Saurabh Bhandari
  • 340
  • 3
  • 11