Pager Adapter not displaying image after getting from response in fragment
Asked Answered
I

2

0

I am getting three images from response,and i am storing in arraylist,then i try to set in PagerAdapter,but nothing display in my view pager,following is my pageradapter class,can any one tell that what is mistake i made?

I am using Fragment

In my Postexecute i am setting it in view pager like this

adapter = new ImageAdapter(All_Product_Details.this.getActivity());
            viewPager.setAdapter(adapter);
}

below is my adapter which i set after asynctask

      public class ImageAdapter extends FragmentPagerAdapter {
    Context context;
    ImageAdapter(Context context)
    {
        this.context=context;
    }
    @Override
    public int getCount() {
        return multimglist.size();
    }

    @Override
    public Fragment getItem(int i) {
        return null;
    }

    @Override
    public void destroyItem(View container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((ImageView) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = new ImageView(context);
        int padding = context.getResources().getDimensionPixelSize(
                R.dimen.activity_horizontal_margin);
        imageView.setPadding(padding, padding, padding, padding);
        //imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        Picasso.with(context).load(multimglist.get(position)).into(imageView);


        // imageView.setImageURI(Uri.parse(imgStr[position]));
        ((ViewPager) container).addView(imageView, 0);
        return imageView;
    }
}
Illsorted answered 17/7, 2015 at 13:19 Comment(0)
D
2
this is my pager adapter change according that
public class LoginViewPagerCA extends PagerAdapter {
    SparseArray<View> views = new SparseArray<>();
    List<Viewpager_POJO> listofPersons;
    LayoutInflater inflater;
    Context context;

    private int count = 4;
    public LoginViewPagerCA(Context context, List<Viewpager_POJO> listofPersons) {
        this.context = context;
        this.listofPersons = listofPersons;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listofPersons.size();
    }
    public void setCount(int count) {
        this.count = count;
    }

    @Override
    public boolean isViewFromObject(View view, Object obj) {
        // TODO Auto-generated method stub
        return view == ((LinearLayout) obj);

    }
    public void setData(ArrayList<Viewpager_POJO> data) {
        listofPersons = data;
    }

    public Object instantiateItem(ViewGroup container, final int position)

    {

        final TextView txtFstName, txtLstName, txtRollno, txtArrTime, txtTimeLeft;
        ImageView imgView;

        final Viewpager_POJO rowperson = listofPersons.get(position);

        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View itemView = inflater.inflate(R.layout.landing_pager_item, container, false);
        txtFstName = (TextView) itemView.findViewById(R.id.pager_big_tv);
        txtLstName = (TextView) itemView.findViewById(R.id.pager_small_tv);
        imgView = (ImageView) itemView.findViewById(R.id.pager_image);


        try {
            // txtCompn.setText((position + 1) + "/" + StuFirstNM.length);
            txtFstName.setText(rowperson.getDesc());
            txtLstName.setText(rowperson.getRating());

            //new LoadImage(imgView).execute(rowperson.getUrls());
            if (!TextUtils.isEmpty(rowperson.getUrls())) {
             //   PicassoTrustAll.getInstance(context).load(rowperson.getUrls()).placeholder(context.getResources().getDr`enter code here`awable(R.drawable.ic_launcher)).error(context.getResources().getDrawable(R.drawable.ic_launcher)).into(imgView);
//
                byte[] imgbytes = Base64.decode(rowperson.getStrImm(), Base64.DEFAULT);
                Bitmap bitmap = BitmapFactory.decodeByteArray(imgbytes, 0, imgbytes.length);
                imgView.setImageBitmap(bitmap);
//                PicassoTrustAll.getInstance(context)
//                        .load(rowperson.getUrls())
//                        .error(R.mipmap.landinglogo)
//                        .into(imgView);
                Log.w("Testing", "Image Download URL=" + rowperson.getUrls());
            } else {
                imgView.setBackgroundResource(R.mipmap.landinglogo);
            }


            ((ViewPager) container).addView(itemView);
            views.put(position, itemView);

        } catch (Exception e)

        {
            e.printStackTrace();
        }

        return itemView;
    }


    public void destroyItem(ViewGroup container, int position, Object object) {
        View view = (View) object;

        ((ViewPager) container).removeView(view);
        views.remove(position);
        view = null;
    }
Dinghy answered 18/7, 2015 at 5:8 Comment(0)
P
1

If you are using Fragments, you should use FragmentPagerAdapter, not PagerAdapter. See official documentation here. You should override getItem(int position) to return the desired Fragment at the proper position.

If you look at the documentation, they show a FragmentPagerAdapter class named MyAdapter. This looks like:

public static class MyAdapter extends FragmentPagerAdapter {

    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

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

    @Override
    public Fragment getItem(int position) {
        return ArrayListFragment.newInstance(position);
    }
}

The getItem(int position) method should return a new instance of a particular Fragment. You are currently returning null, which means that nothing will be displayed. For example, in my own code, I override getItem() like this:

@Override
public Fragment getItem(int position)
{
    switch (position)
    {
    case PagerConstants.PAGE_FILTER_RECIPES:
        return new FilteredRecipesFragment();

    case PagerConstants.PAGE_SELECTED_RECIPES:
        return new SelectedRecipesFragment();

    case PagerConstants.PAGE_SHOPPING_LIST:
        return new ShoppingListFragment();

    default: // Make sure that this never happens.
        return null;
    }
}
Piddling answered 17/7, 2015 at 15:9 Comment(2)
can you help me to get exampleIllsorted
I updated my post. You cannot return null from getItem() as this is where the Fragment in question will be created/reused.Onstad

© 2022 - 2024 — McMap. All rights reserved.