0

I am trying to load a recycler view that lists cards, each card has four elements. two text views(posttext, score are the id) and two buttons(minusOne and plusOne).

The code below loads the textview's content and buttons properly on all cards.

I try to implement the following behavior.

onbuttonclick of plusOne button, the score will be changed and the minusOne button is hidden.

The strange part is, the scores get updated properly on respective cards. But the hiding part appears to happen in multiple cards when the action is performed on one card.(every n+5th card). How do I fix this? Where am I going wrong? Thank you.

P.S : Suggest title edit

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


        ParseObject model = items.get(position);
        viewHolder.posttext.setText(model.getString("postText"));
    viewHolder.score.setText(Integer.toString(model.getInt("score")));



        viewHolder.plusOne.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {


                ParseObject model = items.get(position);

                final int score = model.getInt("score");

                model.increment("score", 1);

                model.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(ParseException e) {

                        if(e==null)
                        {
                            viewHolder.score.setText(Integer.toString(score+1));
                            viewHolder.minusOne.setVisibility(View.GONE);

                        }
                        else{
                            e.printStackTrace();
                           }
                    }
                });
            }
        });
55597
  • 2,033
  • 1
  • 21
  • 40
  • I do not have any experience using RecycleView but the same will happen if you were using an ArrayAdapter or similar. My guess (using knowledge from ArrayAdapter) is that you need to cover all logic branches. Meaning something like `if (score > 0) set visible else set invisible`. This way the visibility is determined each time onBindViewHolder is called, also for recycled views. – cYrixmorten May 30 '15 at 19:03
  • Oh well, just read the answer by yousef, and noticed that his idea is more or less the same. Though instead of keeping track of a list of boolean, I would read the state directly. – cYrixmorten May 30 '15 at 19:09
  • Thanks for taking time to help!! Will give yousef's solution a try. – 55597 May 30 '15 at 19:31

1 Answers1

2

You need to make Arraylist of booleans for the minus button you can set it in your model or in the adapter whatever you want, this arraylist will contains the status of the button so it will be true by default unless the user clicked so it will change to false, and after updating it just notify the adapter by notifyDataSetChanged() it will update the shown views as the array of booleans.

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


    ParseObject model = items.get(position);
    viewHolder.posttext.setText(model.getString("postText"));
    viewHolder.score.setText(Integer.toString(model.getInt("score")));

     if (miusButtonStatus.get(position)) {
        viewHolder.minusOne.setVisibility(TextView.VISIBLE);
     } else {
        viewHolder.minusOne.setVisibility(TextView.GONE);
     }

    viewHolder.plusOne.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            ParseObject model = items.get(position);

            final int score = model.getInt("score");

            model.increment("score", 1);

            model.saveInBackground(new SaveCallback() {
                @Override
                public void done(ParseException e) {

                    if(e==null)
                    {
                        viewHolder.score.setText(Integer.toString(score+1));
                        miusButtonStatus.get(position) = false;
                        notifyDataSetChanged();

                    }
                    else{
                        e.printStackTrace();
                       }
                }
            });
        }
    });
Yousef Zakher
  • 1,634
  • 15
  • 18