0

I have RecyclerView, and in OnClick method I need get Bitmap image of element I clicked:

class ViewHolder extends RecyclerView.ViewHolder {
            ImageView imgSticker;

            ViewHolder(View itemView) {
                super(itemView);
                imgSticker = itemView.findViewById(R.id.imgSticker);

                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mStickerListener != null) {
                            try {
                                URL url = new URL(stickerList.get(getLayoutPosition()));
                                image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                            } catch (MalformedURLException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                            mStickerListener.onStickerClick(image);
                        }
                        dismiss();
                    }
                });
            }
        }

But this doenst work, because I get exception, that it should be done in another tread, I know I need create class extends AsyncTask and override method "doInBackground", but it is difficult for me to understand what parametres I need write here: class downloadStickersTask extends AsyncTask<?, ?, ?> and how I should override doInBackgroundMethod. I am beginner at android, and threads are difficult for me

1 Answers1

0
if (mStickerListener != null) {
        new Thread(() -> {
            
                            try {
                                URL url = new URL(stickerList.get(getLayoutPosition()));
                                image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                            } catch (MalformedURLException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                            
                        }
            runOnUiThread(()->{
                mStickerListener.onStickerClick(image);
            });
        }).start();
dismiss();

try this

Gulab Sagevadiya
  • 872
  • 1
  • 8
  • 20
  • Thanks, but it doesnt work, when I call this method in other class I get ` Can't create handler inside thread Thread[Thread-8,5,main] that has not called Looper.prepare() ` – Daniil Vlasov Jun 20 '21 at 10:14
  • are you using java 8 ? – Gulab Sagevadiya Jun 20 '21 at 10:19
  • I saw it in android studio folder: "This notice is provided with respect to ASM Bytecode Manipulation Framework v5.0.3, which may be included with JRE 8, and JDK 8, and OpenJDK 8." – Daniil Vlasov Jun 20 '21 at 10:29