0

I have the below code trying to use ViewPager.

The problem is the image is not being displayed, I guess the problem is the fragment is not being accessed ( or "called") can you identify why?

I have added a print in the Fragment method to check if it's been accessed.

This the ViewPager adapter, the getItem should return the value of the fragment, or there is something wrong ?

 public class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {
     public static final String ARG_OBJECT = "Person_List";
     private ArrayList<Listitem> personArrayList;

     public DemoCollectionPagerAdapter(FragmentManager fm, ArrayList<Listitem> personArrayList)
     {
         super(fm);
         Log.d("s", "adapterview");
         this.personArrayList = personArrayList;
     }

     @Override
     public Fragment getItem(int i) {
         return DemoObjectFragment.newInstance(this.personArrayList.get(i));
     }

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

This is the rest of the code

public class SingleViewActivity extends FragmentActivity {
    ImageView imageView;

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

        ArrayList<Listitem> personArrayList = new ArrayList<Listitem>();

        ViewPager mViewPager = (ViewPager) findViewById(R.id.pager);
        Log.d("s", "singleview");
        Listitem item = new Listitem("1", "http://developer.android.com/assets/images/android_logo.png");
        personArrayList.add(item);

        DemoCollectionPagerAdapter mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getSupportFragmentManager(), personArrayList);
        mViewPager.setAdapter(mDemoCollectionPagerAdapter);
    }
}

public static class DemoObjectFragment extends Fragment {
    public static final String ARG_PERSON_LIST = "Person_List";
    private Listitem person;

    public static DemoObjectFragment newInstance(Listitem  items) {
        DemoObjectFragment f = new DemoObjectFragment();
        Bundle args = new Bundle();
        args.putParcelable(ARG_PERSON_LIST, items);
        f.setArguments(args);
        System.out.print("instance1");
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();

        if (args.containsKey(ARG_PERSON_LIST)) {
            this.person = args.getParcelable(ARG_PERSON_LIST);
        } else {
            this.person = null;
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(
                        R.layout.fragment_collection_object, container, false);
        ImageView imageView = (ImageView) rootView.findViewById(R.id.funnyimage);

        Bundle args = new Bundle();

        this.person = args.getParcelable("Person_List");

        System.out.print("here1");

        if (person != null) {
            Picasso.with(getActivity())
                .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

1 Answers1

2

Delete these lines

Bundle args = new Bundle();

this.person = args.getParcelable("Person_List");

this.person will be null because you are trying to get the Parcelable from an empty Bundle.

Therefore, this will not be entered.

if (person != null) {

The reason you don't need those lines is because this.person is correctly being loaded inside onCreate. You don't need to assign it from with onCreateView.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Your the man it worked , thanks . now as you remeber we are loading the image staticly. how to receive from the other activity ? ill post a question about it if you want okay ? – Moudiz Jan 15 '16 at 17:01