1

In my activity i'm getting list of images that should be display in a slideshow,

now Source for list of images that should display comes from JSON so I have parse json and got list of images that i need to show from Appfolder/app_content

I have Class:

MainActivity:

  public class MainActivity extends Activity {

        ArrayList<String> imageSlideList = new ArrayList<String>();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

             //Get Intent- Extra
        Intent intent = getIntent();
            String swpImgSImgs = intent.getStringExtra("swpImgS");

         try {
             JSONArray array = new JSONArray(swpImgSImgs);
             for (int i = 0 ; i< array.length(); i++){
                String imageList = array.getString(i);
                FileOperation fo= new FileOperation();
                File   dir=fo.createFileManagerInsidePackage(AppContext.getAppContext());
                String imagePath = dir+"/"+imageList;
                imageSlideList.add(imagePath);
             }
         } catch (JSONException e) {
             e.printStackTrace();
         }  
            ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
            ImageAdapter adapter = new ImageAdapter(this);
            viewPager.setAdapter(adapter);


        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            return false;
        }

    }

Adapter:

    public class SwipingImageSlidesAdapter extends PagerAdapter {



        Context context;

//I need to use files that are in json 
        private int[] GalImages = new int[] {
            R.drawable.camera,
            R.drawable.camera
        };

        SwipingImageSlidesAdapter(Context context){
            this.context=context;
        }
        @Override
        public int getCount() {
            return GalImages.length;
        }

        @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.CENTER_INSIDE);
            imageView.setImageResource(GalImages[position]);
            ((ViewPager) container).addView(imageView, 0);
            return imageView;
        }

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

Now what changes do i have to make to display images from Application-folder(App/app_content) instead of using R.id.imageName using that ArrayList that I created imageSlideList in MainActivity?

Krrishnaaaa
  • 689
  • 11
  • 23
DPP
  • 12,716
  • 3
  • 49
  • 46

3 Answers3

1

you can get the image from aarraylist like way

imageView.setImageResource((int)aimageSlideList.get(position));

pass the arraylist in adapter class

private ArrayList<String> aimageSlideList=null;
private LayoutInflater mInflater=null;


    public SwipingImageSlidesAdapter (Activity context,ArrayList<String> aimageSlideList) {
        super(context, 0);

        mInflater = context.getLayoutInflater();
        this.aimageSlideList=aimageSlideList;
        }
Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
  • But how can I pass arraylist from activity to adapter? – DPP Jul 03 '13 at 04:56
  • pass in constructor of SwipingImageSlidesAdapter – Sunil Kumar Jul 03 '13 at 04:57
  • pass it like `ImageAdapter adapter = new ImageAdapter(this,imageSlideList)` in your constructor receive the parameter. – Raghunandan Jul 03 '13 at 05:03
  • Thanks, But i'm getting: Cannot cast from String to int – DPP Jul 03 '13 at 05:06
  • @sunil you can't set the imageview like that. its a path which is a string and you are casting it to int. – Raghunandan Jul 03 '13 at 05:14
  • @DixitPatel if the images are large in number consider using lazy loading. and also use a view holder.http://developer.android.com/training/improving-layouts/smooth-scrolling.html – Raghunandan Jul 03 '13 at 05:18
  • @DixitPatel for lazy loading check this http://stackoverflow.com/questions/16789676/caching-images-and-displaying/16978285#16978285. instead of harcoded string urls use the arraylist. – Raghunandan Jul 03 '13 at 05:19
1

Pass the list of path to the constructor of the adapter.

     ImageAdapter adapter = new ImageAdapter(this,imageSlideList)

Then in Adapter

      ArrayList<String> mList;
      SwipingImageSlidesAdapter(Context context,ArrayList<String> list){
        this.context=context;
        mList = list; 
    } 

Then in getView

        Bitmap b = BitmapFactory.decodeFile(mlist.get(position));
        imageView.setImageBitmap(b);

public static Bitmap decodeFile (String pathName)

Decode a file path into a bitmap. If the specified file name is null, or cannot be decoded into a bitmap, the function returns null.

Parameters

pathName complete path name for the file to be decoded.

Returns

the resulting decoded bitmap, or null if it could not be decoded.

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0
  1. Just create new Class with getter Setter Of Bitmap/Drawable.

  2. Now set your image Bitmap/Drawable in MainActivity.

  3. In your Adapter class, get that image by position.

Bhoomika Brahmbhatt
  • 7,404
  • 3
  • 29
  • 44