0

in my project i'm stored my drawable images in array like this

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

and here is my array :

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

But i want to show images from url(getting url in database), but my GalleryAdapter allows int array :

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

}

I found this solution but it is not clear how to use this:

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

I know, i will be update my Activity and firstly download bitmap and than store bitmap in array but how can i do this? Thanks for help.

2 Answers 2

1

You can pass array of Strings which will contain the url and use any image loader library like Glide, Picasso .

Here's an example on how to use glide:

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

Apart from it you need to change your adapter and initialize it with ArrayList. Let me know if you have trouble implementing.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for answer, but i must use the GalleryAdapter because, in my application it store the stickers in recycler_view and when i select one of them it pressed stickers on the picture.So if i use your way, can I make all of them?
Yeah anyway my answer is not about adapter and view, it was about image loading library which you use to load images, so for any kind of view if you need to load images you can use these.
0

I'm Solved my problem own my own! Here is the referance : Futurestud.io

firstly i'm change my int[] to String[]

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

then in my GalleryAdapter is change like this :

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


     }

and then convert my string to bitmap :

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

finally i'm getting bitmap :

Bitmap bitmap = getBitmapFromURL(resId);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.