Artur Schwarz
Artur Schwarz

Reputation: 41

RecyclerView first and last item are changed, when clicking only one

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?

Upvotes: 3

Views: 2238

Answers (2)

Saish Khandare
Saish Khandare

Reputation: 1

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.

Upvotes: 0

hamena314
hamena314

Reputation: 3109

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.

Upvotes: 1

Related Questions