在阵列问题中存储URL中的图像

时间:2016-06-24 13:54:44

标签: android arrays android-arrayadapter

在我的项目中,我将我的可绘制图像存储在像这样的数组中

 int[] mLocIds = new int[]{R.mipmap.ic_sticker_01, R.mipmap.ic_sticker_02, R.mipmap.ic_sticker_03,
        R.mipmap.ic_sticker_04, R.mipmap.ic_sticker_05, R.mipmap.ic_sticker_06, R.mipmap.ic_sticker_07,
        R.mipmap.ic_sticker_08};

这是我的数组:

   List<StickerView> mStickers = new ArrayList<>();

但我想显示来自url的图片(在数据库中获取url),但我的GalleryAdapter允许int数组:

public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.ViewHolder> implements View.OnClickListener {

private int[] mResIds;
private int[] mLocIds;

public GalleryAdapter(int[] resIds) {
    this.mResIds = resIds;
}

private OnRecyclerViewItemClickListener mOnItemClickListener = null;

public interface OnRecyclerViewItemClickListener {
    void onItemClick(View view, int resId);
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_gallery, viewGroup, false);
    ViewHolder vh = new ViewHolder(view);
    view.setOnClickListener(this);
    return vh;
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
    viewHolder.imageView.setImageResource(mResIds[position]);
    viewHolder.itemView.setTag(mResIds[position]);
}

@Override
public void onClick(View v) {
    if (mOnItemClickListener != null) {
        mOnItemClickListener.onItemClick(v, (Integer) v.getTag());
    }
}

public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
    this.mOnItemClickListener = listener;
}

@Override
public int getItemCount() {
    return mResIds.length;
}

public static class ViewHolder extends RecyclerView.ViewHolder {
    public ImageView imageView;

    public ViewHolder(View view) {
        super(view);
        imageView = (ImageView) view.findViewById(R.id.item_sticker);
    }
}


public void setData(int[] mLocIds) {
    this.mResIds = mLocIds;
    notifyDataSetChanged();
}

public void setDataStandart(int[] mResIds) {
    this.mResIds = mResIds;
    notifyDataSetChanged();
}

}

我找到了这个解决方案,但目前尚不清楚如何使用它:

 public void setBitmaps(Bitmap... bitmaps) {
    for (int i = 0; i < mImageViews.length; i++) {
        mImageViews[i].setImageBitmap(bitmaps[i]);
    }
}

我知道,我将更新我的Activity并首先下载位图,然后将数据存储在数组中,但我该怎么办呢? 谢谢你的帮助。

2 个答案:

答案 0 :(得分:0)

您可以传递包含网址的字符串数组,并使用GlidePicasso等任何图像加载器库。

以下是如何使用滑行的示例:

Glide.with(context)
            .load(url)
            .centerCrop()
            .placeholder(R.drawable.placeholder)
            .into(viewHolder.imageView);

除此之外,您需要更改适配器并使用ArrayList对其进行初始化。如果您在实施方面遇到困难,请告诉我。

答案 1 :(得分:0)

我解决了我自己的问题!这是referance:Futurestud.io

首先我将我的int []更改为String []

String[] mResIds = new String[]{"http://pngimg.com/upload/scratches_PNG6175.png","http://pngimg.com/upload/tiger_PNG549.png"};

然后在我的GalleryAdapter中改变如下:

public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.ViewHolder> implements View.OnClickListener {

    private String[] mResIds;

    public GalleryAdapter(String[] resIds) {


        this.mResIds = resIds;

    }

    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, String resId);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_gallery, viewGroup, false);
        ViewHolder vh = new ViewHolder(view);
        view.setOnClickListener(this);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {


    //    Uri uri = Uri.parse(String.valueOf(mResIds));
        Context context = viewHolder.imageView.getContext();
        Picasso.with(context)
                .load(mResIds[position])
                .into(viewHolder.imageView);


      //  viewHolder.imageView.setImageResource(mResIds[position]);
        viewHolder.itemView.setTag(mResIds[position]);
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {

            mOnItemClickListener.onItemClick(v, (String) v.getTag());
        }
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public int getItemCount() {
        return mResIds.length;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView imageView;

        public ViewHolder(View view) {
            super(view);
            imageView = (ImageView) view.findViewById(R.id.item_sticker);
        }
    }


    public void setData(String[] mResIds) {
        this.mResIds = mResIds;
        notifyDataSetChanged();
    }

    public void setDataStandart(String[] mResIds) {
        this.mResIds = mResIds;
        notifyDataSetChanged();
    }


     }

然后将我的字符串转换为位图:

public static Bitmap getBitmapFromURL(String urlbit) {
    try {
        URL url = new URL(urlbit);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        // Log exception
        return null;
    }
}

最后我得到了位图:

Bitmap bitmap = getBitmapFromURL(resId);