Android将图像从Url加载到GridView

时间:2015-04-16 08:39:37

标签: android gridview universal-image-loader

在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)*

这就是我调用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;    
            }
}

我想将一些图片从url上传到GridView。如何修改setImagesResource,以便能够使用图像填充Gridview?

4 个答案:

答案 0 :(得分:1)

您可以使用Picasso来执行此操作。

他们页面的示例:

Picasso.with(context).load("http://<url to your image>").into(imageView);

答案 1 :(得分:1)

使用Picasso.

执行此操作的最简单方法
Picasso.with(getContext()).load(mThumbId[position]).centerCrop().into(imageView);

修改

    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
        convertView = 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();
}

答案 2 :(得分:1)

在CustomAdapter.class上:

    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",
    };  

    Picasso.with(mContext).load(mThumbIds[0]).centerCrop().into(imageView);

    return imageView;

private Integer[] mThumbIds =
{
    R.drawable.ic_stub , R.drawable.ic_stub,
    R.drawable.ic_stub , R.drawable.ic_stub,
    R.drawable.ic_stub , R.drawable.ic_stub ,
    R.drawable.ic_stub , R.drawable.ic_stub,
    R.drawable.ic_stub , R.drawable.ic_stub,
    R.drawable.ic_stub , R.drawable.ic_stub,
};

现在,它打印第一张照片6次!

答案 3 :(得分:0)

从URL下载图像的最简单方法是,使用AQuery Library下载AQuery.jar文件将其放入Llib文件夹

//load an image to an ImageView from network
AQuery aq=new AQuery(this);   
aq.id(R.id.imageview).image("http://www.vikispot.com/z/images/vikispot/android-w.png");

请参阅此信息以获取更多信息 https://code.google.com/p/android-query/wiki/ImageLoading