3

I'm using a recyclerview to display a list of interests one could choose from. Clicking the very first item makes the very last item also selected

Selecting first item:

RecyclerView first item selected

Last item is also selected:

RecyclerView last item selected

The selection is done with this code:

@Override
public InterestViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    TextView v = (TextView) LayoutInflater.from(parent.getContext())
            .inflate(R.layout.interests_textview, parent, false);
    v.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TextView textView = (TextView) v;
            if (textView.getCompoundDrawables()[2] == null) {
                textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.checkmark, 0);
            } else {
                textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
            }
        }
    });
    return new InterestViewHolder(v);
}

Also the very first item is also selected, when clicking the very last item. Who knows what could cause this?

hamena314
  • 2,969
  • 5
  • 30
  • 57

2 Answers2

1

You are inflating a layout into a TextView. Change it to View v and it should work:

View v = LayoutInflater.from(parent.getContext())
        .inflate(R.layout.interests_textview, parent, false);

Also you need to use findViewByID() in order to get the correct TextView from your layout:

Instead of

TextView textView = (TextView) v;

Do this:

TextView textView = findViewById(<id of your textview in the layout>);

Casting the View to a TextView is not what you want to do.

hamena314
  • 2,969
  • 5
  • 30
  • 57
  • None of your tips were helpful, i'm afraid to say. But i did manage to resolve this issue, by just saving the click state in an object, then checking the status (chosen/not chosen) in onBindViewHolder. The root cause of this issue was that RecyclerView had recycled the view and hence added a checkmark on the next recycled item. – Artur Schwarz May 08 '18 at 19:53
  • Your castings definitely are not 100% correct, but if you found a solution, you could write an answer to our own question and later accept it. This will help the next user with a similiar problem. I then will delete my answer, so your comment wont be lost until then. – hamena314 May 09 '18 at 07:01
0

I have a Checkbox to select items and the following String Arrays

data : holds all my items checkedData: holds all checked items

To solve this try editing the onBindViewHolder(holder , position)

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

    String title =data[position];
    holder.textTitle.setText(title);

    //logic to solve first and last (multiple) selection issue
    CheckBox chk = holder.chk;
    String current  = data[position];
    chk.setChecked(false);
    for(String st : checkedData){
        if(st.equals(current)){
            chk.setChecked(true);
        }
    }

    holder.setItemClickListener(new ItemClickListener()
    {
        @Override
        public void onItemClick(View v, int pos)
        {
            //here analomay occured 19:36 Custom Reycler view CheckBoxes | anomaly is now cleared.
            CheckBox chk = (CheckBox) v;//.findViewById(R.id.chk);

            if(chk.isChecked())
            {
                   checkedData.add(data[pos]);
            }else if(!chk.isChecked())
            {
                checkedData.remove(data[pos]);
            }



        }
    });
}

Explanation

What it will do is every time the data gets binded to holder in onBindViewHolder you previously uncheck the checkbox and then check if the corresponding string of that holder is present in checkedData array if yes set checkBox to true.