1

I used to get Bundle in activity like this and it worked fine

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.singleview);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // list = (ListView) findViewById(R.id.ListView);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
   //     setSupportActionBar(toolbar);

     //   getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        imageView = (ImageView) findViewById(R.id.funnyimage);
        Bundle bundle = getIntent().getExtras();


        ArrayList<Listitem> personArrayList = bundle.getParcelableArrayList("Person_List");

        if (personArrayList != null && !personArrayList.isEmpty()) {
            for (Listitem person : personArrayList) {
                Picasso.
                        with(this).
                        load(person.url)
                        .placeholder(R.drawable.logo)
                        .fit()
                        .noFade()
                        .into(imageView);
                Log.i("PersonsActivity",String.valueOf(person.url));
            }
        }

My problem is I want to get arraylist paracble in fragment

I am having error in word bundle in the below code

    ArrayList<Listitem> personArrayList = bundle.getParcelableArrayList("Person_List");

I though using instance:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //change to avoid orientation crash
    imageResourceId = getArguments().getInt("param");
    numberSelected = getArguments().getParcelableArrayList("number"); // this is wrong but i was trying something
}
public Fragment newInstance(int imageResourceId, int numberSelected) {
    DemoObjectFragment f = new DemoObjectFragment();
    f.imageResourceId = imageResourceId;
    f.numberSelected = numberSelected;
}

this is my code for your reference

public class SingleViewActivity extends FragmentActivity {
    ImageView   imageView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page_view);
        DemoCollectionPagerAdapter
        mDemoCollectionPagerAdapter =
                new DemoCollectionPagerAdapter(
                        getSupportFragmentManager());
        ViewPager    mViewPager = (ViewPager) findViewById(R.id.pager);
           imageView = (ImageView) findViewById(R.id.funnyimage);
        Bundle bundle = getIntent().getExtras();

        mViewPager.setAdapter(mDemoCollectionPagerAdapter);
    }

    /*  if (savedInstanceState == null) {
            // During initial setup, plug in the details fragment.
            MyFragment myrag = new MyFragment();
            myrag.setArguments(getIntent().getExtras());
            getSupportFragmentManager().beginTransaction().add(android.R.id.content, myrag).commit();
        }
*/
        // Since this is an object collection, use a FragmentStatePagerAdapter,
        // and NOT a FragmentPagerAdapter.

        public class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {
            public DemoCollectionPagerAdapter(FragmentManager fm) {
                super(fm);
            }

            @Override
            public Fragment getItem(int i) {
                Fragment fragment = new DemoObjectFragment();
                Bundle args = new Bundle();
                // Our object is just an integer :-P
                args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1);
                fragment.setArguments(args);
                return fragment;
            }

            @Override
            public int getCount() {
                return 100;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return "OBJECT " + (position + 1);
            }
        }

// Instances of this class are fragments representing a single
// object in our collection.
        public static class DemoObjectFragment extends Fragment {
            public static final String ARG_OBJECT = "object";
    ImageView   imageView;
    int imageResourceId;
    int numberSelected;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //change to avoid orientation crash
        imageResourceId = getArguments().getInt("param");
        numberSelected = getArguments().getInt("number");
    }
    public Fragment newInstance(int imageResourceId, int numberSelected) {
        DemoObjectFragment f = new DemoObjectFragment();
        f.imageResourceId = imageResourceId;
        f.numberSelected = numberSelected;
    }
            @Override
            public View onCreateView(LayoutInflater inflater,
                                     ViewGroup container, Bundle savedInstanceState) {
                // The last two arguments ensure LayoutParams are inflated
                // properly.

                View rootView = inflater.inflate(
                        R.layout.fragment_collection_object, container, false);
                Bundle args = getArguments();
             //   ((TextView) rootView.findViewById(R.id.text1)).setText(Integer.toString(args.getInt(ARG_OBJECT)));



                ArrayList<Listitem> personArrayList = bundle.getParcelableArrayList("Person_List");

                if (personArrayList != null && !personArrayList.isEmpty()) {
                    for (Listitem person : personArrayList) {
                        Picasso.
                                with(mcontext).
                                load(person.url)
                                .placeholder(R.drawable.logo)
                                .fit()
                                .noFade()
                                .into(imageView);
                        Log.i("PersonsActivity",String.valueOf(person.url));
                    }
                }
                return rootView;
            }
        }
    }
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Moudiz
  • 7,211
  • 22
  • 78
  • 156
  • Is there a question here? – OneCricketeer Jan 09 '16 at 22:11
  • @cricket_007 sorry , the full question wasent posted correctly due to connection,I edit my question please check it now – Moudiz Jan 09 '16 at 22:15
  • I don't see where you set the arraylist into the Fragment arguments. Please refer to this post http://stackoverflow.com/questions/9245408/best-practice-for-instantiating-a-new-android-fragment – OneCricketeer Jan 09 '16 at 22:20
  • @cricket_007 if I want to add `int` I do like this `args.putInt("someInt", someInt);` if it was parcable , what to add ? args.getparacble("");? – Moudiz Jan 09 '16 at 22:25
  • Why are you talking about ints? Your question says "arraylist". If you use `putInt` use `getInt`... An `int` isn't `Parcelable` since it isn't an object – OneCricketeer Jan 09 '16 at 22:28
  • Can you provide complete DemoObjectFragment .class and code that you call DemoObjectFragment.newInstance()? – tiny sunlight Jan 10 '16 at 03:32
  • @cricket_007 I am sorry but can you explain for me which array list should be changed ? i checked them and all of them are `ArrayList` – Moudiz Jan 11 '16 at 21:31
  • You literally have 5 questions over the past day that essentially address the same set of problems. Which one do you want answered? – OneCricketeer Jan 11 '16 at 21:34

1 Answers1

2

It works for me, if it doesn't work for you post the error message from the logcat,

ArrayList<Listitem> personArrayList;

//To put your arraylist
Bundle b = new Bundle();
b.putParcelableArrayList("Person_List", personArrayList);

//To get your arraylist
Bundle extras = getIntent().getExtras();
personArrayList = extras.getParcelableArrayList("Person_List");
Coder
  • 106
  • 1
  • 7