Search code examples
androidandroid-viewpager

Android ViewPager loading same images


I am using ViewPager to load images from server.When it is swiped the first image is shown again and again.I checked the urls in the browser and it shows different images.I then tried loading images from drawable.Here the last image is being loaded repeatedly.Below is the complete code:

ImagePagerAdapter.java:

 public class ImagePagerAdapter extends PagerAdapter {
    TaxiDisplayData taxiDisplayData;
    LinearLayout pager_lyt;
    ImageView imageView;
    LayoutInflater layoutInflater;
    Context context;
    String category;
    int count;
    LinearLayout linearLayout;
    View view;
    ImageLoaderConfiguration config;
    ArrayList images;

    public ImagePagerAdapter(Context context,String ctgry){
        Log.e("I","T");
        config = new ImageLoaderConfiguration.Builder(context).build();
        ImageLoader.getInstance().init(config);
        images =new ArrayList();
        this.context=context;
        this.category=ctgry;
        if (category.equalsIgnoreCase("taxi")){
            count= taxiDisplayData.getInstance().imagecount();
        }
        layoutInflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }
    @Override
    public int getCount() {

        Log.e("Count",Integer.toString(count));
        return count;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        view=layoutInflater.inflate(R.layout.srch_dsply_pager,container,false);
        linearLayout=(LinearLayout) view.findViewById(R.id.pgr_lyt);
         imageView=(ImageView) view.findViewById(R.id.image1);
        if (category.equalsIgnoreCase("taxi"))
        {  images =taxiDisplayData.getInstance().getArrayList();
            if(count!=0){

                Iterator iterator= images.iterator();
                while (iterator.hasNext())
                {
                    Log.e("I","Z");
                    Picasso.with(context).load("http://example.com/xxxx/"+(String) iterator.next())
                            .placeholder(R.drawable.doctors)
                            .fit().into(imageView);
                }
            }}
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View)object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        Log.e("I","f");
        return view==object;
    }}

srch_dsply_pager.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/pgr_lyt">
<ImageView
    android:layout_width="match_parent"
    android:layout_height="@dimen/prsnl_img_dimen"
    android:id="@+id/image1"
    android:scaleType="fitXY"
   />
</LinearLayout>

Solution

  • instantiateItem() Creates one view and adds it in the view pager. In your source you are iterating trough the whole array, load only the last one and return the view. Instead the while you should have something like

    String url = images.get(position);
    Picasso.load(url).into(imageview);