-1

I am using logic for Like and Dislike Button

where I have declared variable

var user_like_dislike_state == 0

When I click on Like button value change to one if I pressed again it will change back to 0

if(user_like_dislike_state == 0 || user_like_dislike_state == 1){
user_like_dislike_state = -1
} else {
user_like_dislike_state = 0
}

Its working when I try to use on Single post but when I try to use this logic in Recyclerview

If I liked post 1 then I try to like post 4 then it automatically change value to 0

In which 3 States I have given

user_like_dislike_state == 1 : **Like**

user_like_dislike_state == 0 : **Non**

user_like_dislike_state == -1 : **Dislike**

Adapter.kt:

class ViewsinglepostAdapter(var commentsList: ArrayList<Comment>, val upVote : View.OnClickListener, val downVote: View.OnClickListener): RecyclerView.Adapter<ViewsinglepostAdapter.ViewHolder>(){
    var user_like_dislike_state = 0
    var comment_id = 0

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.user_name?.setText(commentsList.get(position).User)
        holder.comment?.setText(commentsList.get(position).comment)
        holder.upvote?.setOnClickListener({
            if(user_like_dislike_state == 0 || user_like_dislike_state == -1){
                user_like_dislike_state = 1
            } else {
                user_like_dislike_state = 0
            }
            comment_id = commentsList.get(position).Id
            upVote.onClick(it)
        })
        holder.downvote?.setOnClickListener({it->
            if(user_like_dislike_state == 0 || user_like_dislike_state == 1){
                user_like_dislike_state = -1
            } else {
                user_like_dislike_state = 0
            }
            comment_id = commentsList.get(position).Id
            downVote.onClick(it)
            user_like_dislike_state == 0
        })
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder{
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.singlecomment,parent,false)
        return ViewHolder(itemView)
    }

    override fun getItemCount(): Int {
        return commentsList.count()
    }


    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
        var upvote: ImageView? = null
        var downvote: ImageView? = null
        var user_name: TextView? = null
        var comment: TextView? = null
        init {
            this.upvote = itemView.findViewById(R.id.upvote_post_img)
            this.downvote = itemView.findViewById(R.id.down_vote_post_img)
            this.user_name = itemView.findViewById(R.id.user_name)
            this.comment = itemView.findViewById(R.id.comment)
        }
    }
}
piet.t
  • 11,718
  • 21
  • 43
  • 52
Ashish
  • 6,791
  • 3
  • 26
  • 48
  • 2
    first of all I suggest you to use `enums` instead of literal values - http://kotlinlang.org/docs/reference/enum-classes.html Second - how do you keep state for each row? If it's kept globally the no wonder it behaves strangely – Antek Dec 04 '18 at 09:23
  • you can refer this link https://stackoverflow.com/questions/45550090/like-unlike-button-recyclerview-image – Android Dec 04 '18 at 09:25

5 Answers5

2

You just take one boolean flag and set its initial value as false . When button clicked you just update its value by use of turnery operator to get your value

user_like_dislike_state = user_like_dislike_state?false:true;

//For upvote button you should set one or -1 .

holder.upvote?.setOnClickListener({
user_like_dislike_state = user_like_dislike_state<1?1:-1;
comment_id = commentsList.get(position).Id
upVote.onClick(it)
})

for downvote button you check it already down-voted or upvoted.

holder.downvote?.setOnClickListener({it->
user_like_dislike_state = user_like_dislike_state>-1?-1:0;
comment_id = commentsList.get(position).Id
downVote.onClick(it)
})
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
  • what if i want to give null also means if user dont want to like story but he also dont want to dislike means for null there is need to be any value – Ashish Dec 04 '18 at 09:37
  • @Ashish how and when you send null to this parameter , because you have only one button to update value of user_like_dislike_state . – Chetan Joshi Dec 04 '18 at 09:40
  • nope there are two button similar to reddit i have given in which if i click upvote it will give 1 point and if i click one downvote it will put value 0 The code i have provided work for single post – Ashish Dec 04 '18 at 09:42
1

Maybe because of that Recyclerview, I have an assumption there's a post/row that is gone when you scroll it down or scroll it up. Every view/row that is gone in Recyclerview when it's scrolled up or down it will be reloaded again, so that's why you always got 0

0

create class with member user_like_dislike_state:Int pass ArrayList of that class to recycle adapter and handled each row view separately in list.

asim
  • 310
  • 1
  • 8
0

I would advise using a local variable when liking/disliking otherwise var user_like_dislike_state will retain the state of the previous like/dislike unless you reset it

Blue Jones
  • 385
  • 1
  • 9
0

It very simple just take a variable in model class example:- var like_Status and set it accordingly on click in the recyclerview and when your scrolling the list check that variable like

if(like_statusbutton==-1)
{ 
setLikeStatus="dislike"
}
else if(like_status==1)
 {
 setLikeStatus="like"
}
else {
 setLikeStatus="none"
}

you have to maintain the status for each like button in the model class. that's why you are facing this issue in your code. please maintain the status of every like button in the model class and at the time of scrolling in recycleview check the previous state in of the button.