Search code examples
androidgridviewuniversal-image-loader

Android Load image from Url to GridView


In CustomAdapter.class :

    public View getView(int position, View convertView, ViewGroup parent) {

    ImageView imageView;

        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(4, 4, 4, 4);
        } else
            {
                imageView = (ImageView) convertView;
            }


        String[] mThumbIds  =
        {   "https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg",
            "https://lh4.googleusercontent.com/--dq8niRp7W4/URquVgmXvgI/AAAAAAAAAbs/-gnuLQfNnBA/s1024/A%252520Song%252520of%252520Ice%252520and%252520Fire.jpg",
            "https://lh5.googleusercontent.com/-7qZeDtRKFKc/URquWZT1gOI/AAAAAAAAAbs/hqWgteyNXsg/s1024/Another%252520Rockaway%252520Sunset.jpg",
        };


        imageView.setImageResource(mThumbIds[position]);

        return imageView;
}


I get this error: *The method setImageResource(int) in the type ImageView is not applicable for the arguments (String)*

This is how I call CustomAdapter.class:

public static class ImageFrg extends Fragment
{

            GridView gridview;
            DisplayImageOptions options;

            public ImageFrg () {


            }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
            {
                View rootView = inflater.inflate(R.layout.imageFrg, container, false);          
                gridview=(GridView) rootView.findViewById(R.id.gridview);
                CustomAdapter i = new CustomAdapter(getActivity());
                gridview.setAdapter(i);   
                return rootView;    
            }
}

I want to upload some images from url into a GridView. How can I modify the setImagesResource, to be able to fill the Gridview with images ?


Solution

  • Easiest way to do this using Picasso.

    Picasso.with(getContext()).load(mThumbId[position]).centerCrop().into(imageView);
    

    EDIT

        String[] mThumbIds  =
        {   "https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg",
            "https://lh4.googleusercontent.com/--dq8niRp7W4/URquVgmXvgI/AAAAAAAAAbs/-gnuLQfNnBA/s1024/A%252520Song%252520of%252520Ice%252520and%252520Fire.jpg",
            "https://lh5.googleusercontent.com/-7qZeDtRKFKc/URquWZT1gOI/AAAAAAAAAbs/hqWgteyNXsg/s1024/Another%252520Rockaway%252520Sunset.jpg",
        };
    
    public View getView(int position, View convertView, ViewGroup parent) {
    
        ImageView imageView;
    
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(4, 4, 4, 4);
        } 
        else {
                imageView = (ImageView) convertView;
        }
    
    
        Picasso.with(mContext).load(mThumbs[position]).centerCrop().into(imageView);
        return imageView;
    }
    
    @Override
    public int getCount() {
       return mThumbId.size();
    }