0

I am sending arraylist from gridviewadapter to the fragment but data its not displayed. the gridview code was working when sending arraylist to an activity, so the problem is with the fragment

this is the grid view adapter

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    System.out.println("entering adapter1");

    View row = convertView;
  final  ViewHolder holder;


    if (row == null) {
        LayoutInflater inflater = LayoutInflater.from(mcontext);
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new ViewHolder();
        holder.imageTitle = (TextView) row.findViewById(R.id.text);
        holder.imageView = (ImageView) row.findViewById(R.id.imageView);
        row.setTag(holder);
    } else {
        holder = (ViewHolder) row.getTag();
    }
    final Listitem item = getItem(position);
    System.out.println("item.getUrl() ");
    System.out.println(item.getUrl());
    Picasso.with(mcontext).setIndicatorsEnabled(true);
    holder.imageTitle.setText(item.getId());

   /* Picasso.
            with(mcontext).
            load(item.getUrl())
            .placeholder(R.drawable.logo)
            .fit()
            .noFade()
            .into(holder.imageView)
            ;*/
    //int maxSize = 4 * 1024 * 1024; // 4MiB

    //Picasso myNewPicasso = new Picasso.Builder(mcontext).memoryCache(new LruCache(maxSize)).build();

    Picasso.with(mcontext)
            .load(item.getUrl())
            .placeholder(R.drawable.logo)
            .fit()
            .noFade()
            .into(holder.imageView);
    holder.imageView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            ArrayList<Listitem> personArrayList = new ArrayList<>();
            personArrayList.add(new Listitem(item.getId(), item.getUrl()));

            Intent intent = new Intent(mcontext,SingleViewActivity.class);
            intent.putExtra("Person_List", personArrayList);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mcontext.startActivity(intent);

            Log.d("OnImageButton", "Clicked");
        }
    });


    return row;
}

static class ViewHolder {
    TextView imageTitle;
    ImageView imageView;
}

this is the fragment that contain also a view pager

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();
        Log.d("s","singleview");
        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);
                Log.d("s","adapterview");
            }

            @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
        Log.d("s","fragview");

    }
    public static Fragment newInstance(Context context,ArrayList<Listitem> personArrayList, int numberSelected) {
        DemoObjectFragment f = new DemoObjectFragment();
        Bundle args= new Bundle();

        args.putParcelableArrayList("Person_List", personArrayList);
        //args.putInt("",imageResourceId);
        f.setArguments(args);
        return f;
    }
            @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)));
                Bundle args= new Bundle();



                ArrayList<Listitem> personArrayList = args.getParcelableArrayList("Person_List");
                System.out.print(personArrayList);

                System.out.print("here1");

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

A bit explanation of what I am doing
In my main activity I have a grid view of image, when I click on one image , I get the single image-clicked on a new activity. I used that with parcelable arraylist that send data from gridview to singleview acitivity. it was working for the activity. but when I implemented the viewpager and the fragment and fragment adapter nothing is showing I dont know why,

this is my logcat , as you notice its not proceeding to the rest,fragview is a log print added in my code.

01-11 20:54:40.814 3323-3323/com.justedhak.www.i D/Activity: performCreate Call secproduct feature valuefalse
01-11 20:54:40.814 3323-3323/com.justedhak.www.i D/Activity: performCreate Call debug elastic valuetrue
01-11 20:54:40.829 3323-3323/com.justedhak.www.i D/s: fragview
01-11 20:54:40.839 3323-3323/com.justedhak.www.i D/s: fragview
01-11 20:54:40.954 3323-3323/? I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@211f949f time:9248062

this is part of the fragment code in Singleviewactivity.java

        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
    Log.d("s","fragview");

}
public static Fragment newInstance(Context context,ArrayList<Listitem> personArrayList, int numberSelected) {
    DemoObjectFragment f = new DemoObjectFragment();
    Bundle args= new Bundle();

    args.putParcelableArrayList("Person_List", personArrayList);
    //args.putInt("",imageResourceId);
    f.setArguments(args);
    return f;
}
        @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)));
            Bundle args= new Bundle();



            ArrayList<Listitem> personArrayList = args.getParcelableArrayList("Person_List");
            System.out.print(personArrayList);

            System.out.print("here1");

            if (personArrayList != null && !personArrayList.isEmpty()) {
                for (Listitem person : personArrayList) {
                    Picasso.
                            with(getActivity()).
                            load(person.url)
                            .placeholder(R.drawable.logo)
                            .fit()
                            .noFade()
                            .into(imageView);
                    Log.i("PersonsActivity",String.valueOf(person.url));
                }
            }
            return rootView;
        }
    }
}
Moudiz
  • 7,211
  • 22
  • 78
  • 156
  • I think the problem is that you never retrieve your "Person_List" from the intent at the SingleViewActivity. You create your fragment, and inside your fragment you use getParcelableArrayList("Person_List"). But at this point it will be null because you never put it in the Fragment. You need to put it in the fragment when you create the Fragment object at the SingleViewActivity. – TerNovi Jan 11 '16 at 19:56
  • Where in the fragment can you please tell me in an answer where to add it in my code – Moudiz Jan 11 '16 at 20:01

1 Answers1

1

Moudiz,

You need to change your DemoCollectionPagerAdapter constructor to accept the ArrayList of people as such:

DemoCollectionPagerAdapter(FragmentManager fm, ArrayList<Listitem> personArrayList)

With the new constructor be sure to pass this to your adapter object when you first create it at the SinglePageActivity View.

public class SingleViewActivity extends FragmentActivity {
    ImageView imageView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page_view);

        Bundle bundle = getIntent().getExtras();

        ArrayList<Listitem> personList = getIntent().getParcelableArrayListExtra("Person_List");

        // Pass it here using your new constructor. 
        DemoCollectionPagerAdapter mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getSupportFragmentManager(), personList); 

        ViewPager mViewPager = (ViewPager) findViewById(R.id.pager);
        imageView = (ImageView) findViewById(R.id.funnyimage);

        Log.d("s","singleview");
        mViewPager.setAdapter(mDemoCollectionPagerAdapter);
    }

The next part is for you to figure out because it is rather confusing why you are passing in your ArrayList when you create a Fragment object here :

public static Fragment newInstance(Context context,ArrayList<Listitem> personArrayList, int numberSelected) {

Let me know if this helps.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
TerNovi
  • 390
  • 5
  • 16
  • I am having error at getintent `new DemoCollectionPagerAdapter( getSupportFragmentManager(), getIntent.getParcelableArrayList("Person_List"));` – Moudiz Jan 11 '16 at 20:56
  • Sorry it should be getIntent().getParcelableArrayListExtra("Person_List"). Check my edit, I just corrected it. – TerNovi Jan 11 '16 at 21:11
  • I was afraid that youll take time to asnwer so I have posted the error in another question please if you can follow it http://stackoverflow.com/questions/34730966/error-while-using-getintent-in-getparcelablearraylist?noredirect=1#comment57208593_34730966 – Moudiz Jan 11 '16 at 21:19
  • *Why you are passing in your ArrayList when you create a Fragment object here* - Refer to this post - http://stackoverflow.com/questions/9245408/best-practice-for-instantiating-a-new-android-fragment – OneCricketeer Jan 11 '16 at 21:32
  • Did you figure it out? I was about to type response and you deleted :( – TerNovi Jan 11 '16 at 21:33
  • @TerNovi yes I delete to focus on your this answer and your question. post your answer as an edit here please. – Moudiz Jan 11 '16 at 21:38
  • @cricket_007 you are right I should read the link you have provided. i am new to android and i am trying to understand how to use fragment with viewpager thats why I am bit lost. – Moudiz Jan 11 '16 at 21:40
  • @Moudiz - I have formatted this code. It looks like it will do what you want to get the arraylist into the PagerAdapter. – OneCricketeer Jan 11 '16 at 22:02