我正在创建一个应用程序,动态加载脚本中的图片,并在网格视图中显示它们。
这是密码。

public class ImageAdapter extends BaseAdapter {

    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }
    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    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(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }


    private String[] mThumbIds = new String[]{
            "http://localhost/comic_app/index.php?id=1",
            "http://localhost/comic_app/index.php?id=67",
            "http://localhost/comic_app/index.php?id=4",
            "http://localhost/comic_app/index.php?id=89",
            "http://localhost/comic_app/index.php?id=98",
            "http://localhost/comic_app/index.php?id=23",
            "http://localhost/comic_app/index.php?id=45",
    };
}

我得到编译时错误
imageview.setimageresource(mthumbids[位置]);
错误:无法应用于(java.lang.string)
我知道setimageresource想得到int,但怎么做。
提前谢谢

最佳答案

imageView.setImageResource()需要来自Drawable的图像,这是一个int,例如:R.drawable.sample
您可以直接从url加载图像;为此,必须使用类似于Picassoimage loading library
使用

Picasso.with(context).load(mThumbIds[position]).into(imageView);

将此添加到build.gradle文件中:
 compile 'com.squareup.picasso:picasso:2.5.2'

关于android - Android中的setImageResource,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35548110/

10-10 07:12