How to Save RecyclerView Buttons Clicked Respectfully - Android

I have a RecyclerView that is filled with CardViews. Each of the CardViews has a button that votes for the message.

This is what the button looks like when not pressed,

up vote - not pressed

This is what the button looks like when clicked,

up vote - pressed

My code works to achieve this, but I have a problem as it is a RecyclerView. When I view the posts, the RecyclerView is recycling the previous posts that were upvoted. Thus, the message will show that it voted even though the user never voted for it.

How can I use the buttons for each CardView with care?

This is my adapter

public class DiscoverRecyclerAdapter
        extends RecyclerView.Adapter<DiscoverRecyclerAdapter.ViewHolder> {

    private String[] mDataset;

    Typeface customFont;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTitle;
        public TextView mVoterCounter;
        public ImageButton mVoterButton;
        public ViewHolder( v) {
            mTitle = (TextView) v.findViewById(;
            mVoterCounter = (TextView) v.findViewById(;

            //Initialize voter button
            mVoterButton = (ImageButton)v.findViewById(;

            mVoterButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {

    // Provide a suitable constructor (depends on the kind of dataset)
    public DiscoverRecyclerAdapter(String[] myDataset, Typeface passedFont) {
        mDataset = myDataset;
        customFont = passedFont;

    // Create new views (invoked by the layout manager)
    public DiscoverRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_discover, parent, false);
        // set the view size, margins, paddings and layout parameters
        return new ViewHolder((;

    // Replace the contents of a view (invoked by the layout manager)
    public void onBindViewHolder(ViewHolder holder, int position) {

    // Return the size of your dataset (invoked by the layout manager)
    public int getItemCount() {
        return mDataset.length;



source to share

2 answers

along with mDataset you will also need a boolean array: mIsSelected will now size this equal to the size of the mDataSet array, or create a class if you want.

Then in onBindViewHolder do like



and click onclick button inside onBindViewHolder like below

 holder.mVoterButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
              mIsSelected[position] = true;




You need to clear the view of previous rows of all previous data at the start of onBindViewHolder.

In your case, it seems that you need to clear all the visibility settings of the view components to what you think is standard. After that, launch the data card.

Being that your passed in dataset is just a string, you need to either call your API or get the invoice / status. Or change your dataset to your own array of objects that tracks all the components needed to customize and write data for each card.

In short: as species come back, you need to clean them up before reusing them.



All Articles