0

RecyclerView used in project. card view uses Imageview, ImageButton, TextView. When user click on ImageView stored image link in objectbox database. Two image button display for like and unlike. when user click like or unlike imagebutton stored like and unlikein database.

how to stored image and like and unlike in database through different click listener.

public class ThumbnailAdapter extends RecyclerView.Adapter<ThumbnailAdapter.MyViewHolder>{

    private Context mContext;
    private List<Thumbnail> albumList;
    private List<Giphy> giphyList;

    private BoxStore boxStore;
    private Box<Giphy> box;

    public class MyViewHolder extends RecyclerView.ViewHolder {
       // public TextView title, count;

        @BindView(R.id.imgThumbnail)
        public ImageView imageView;

        @BindView(R.id.imgButtonThumbUp)
        public ImageButton imageButtonUP;

        @BindView(R.id.imgButtonThumbDown)
        public ImageButton imageButtonDown;

        @BindView(R.id.tvThumbUpCount)
        public TextView tvUpCount;

        @BindView(R.id.tvThumbDownCount)
        public TextView tvCountDown;

        int countUP = 0;
        int countDown = 0;

        String url;
        int id = 0;
        int thumbUp,thumbDown;

        public MyViewHolder(View view) {
            super(view);
            mContext = view.getContext();
            //imageView = (ImageView) view.findViewById(R.id.imgThumbnail);

            ButterKnife.bind(this,view);




            int idCount = id++;

            giphyList = new ArrayList<>();


            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   // String Title = title.getText().toString();

                    Thumbnail list = albumList.get(getAdapterPosition());

                    url = list.getVideoUrl();
                    giphyList.add(new Giphy(0,url,thumbUp,thumbDown));
                    Intent intent = new Intent(mContext,ExoPlayer.class);
                    intent.putExtra("url",list.getVideoUrl());


                    mContext.startActivity(intent);


                }
            });

            imageButtonUP.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                   thumbUp = countUP++;

                    //box.put(countUP);

                    tvUpCount.setText(""+countUP);
                }
            });

            imageButtonDown.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    thumbDown = countDown++;

                   // box.put(countDown);

                    tvCountDown.setText(""+countDown);
                }
            });


            addData(new Giphy(0,url,thumbUp,thumbDown));


        }



    }

    public ThumbnailAdapter(Context mContext, List<Thumbnail> albumList) {
        this.mContext = mContext;
        this.albumList = albumList;
//        this.giphyList = giphyList;


        boxStore = ((ObjectBox)mContext.getApplicationContext()).getBoxStore();
        box = boxStore.boxFor(Giphy.class);

    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_list_item_album, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {


        Thumbnail album = albumList.get(position);



        Glide.with(mContext)
                .asGif()
                .load(album.getGif())
                .apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL))


                .into(holder.imageView);


    }

    @Override
    public int getItemCount() {
        return albumList.size();
    }

   public void addData(Giphy giphy){
        box.put(giphy);
    }
3
  • Please read SQLite Database Doc Commented Sep 21, 2018 at 9:39
  • not used Sqlite datase. used objectbox. Commented Sep 21, 2018 at 9:46
  • My question is Imageview and imagebutton clicklistner event how to get data for stored database Commented Sep 21, 2018 at 9:47

1 Answer 1

1

In onBindViewHolder, set the position as a tag of the ImageButton:

holder.imageButtonUp.setTag(position);
holder.imageButtonDown.setTag(position);

Then in your onClickListener of the ImageButton, you can get the positions:

int position = (Integer)view.getTag();
Thumbnail album = albumList.get(position);

Then do whatever you want to do.

But as you are using Glide, and Glide uses ImageView's tag, so you should inform Glide to use another tag. See this:

Error "You must not call setTag() on a view Glide is targeting" when use Glide

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

1 Comment

Diffterent onclickListner Imagebutton,imageview get data and passed in addData(new Giphy(0,url,thumbUp,thumbDown));

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.