Home > Mobile >  Recycler View with Checkbox issue
Recycler View with Checkbox issue

Time:09-08

Checkbox unchecked after scrolling down and scrolling up, also if i checked one item another item will be checked automatically in lower part of the page.i followed many tutorial but nothing worked for me, someone please help me, i'm stuck with this problem so long. here is my adapter code.


    private Context mCtx;
    private List<TasModel> tasList;
    SparseBooleanArray itemStateArray= new SparseBooleanArray();
    Tasadapter() {
    }

    public Tasadapter(Context mCtx, List<TasModel> tasList) {
        this.mCtx = mCtx;
        this.tasList = tasList;
    }

    @NonNull
    @Override
    public TasviewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
        LayoutInflater inflater =LayoutInflater.from(mCtx);
        View view = inflater.inflate(R.layout.tas_list,null);
        TasviewHolder holder =new TasviewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final TasviewHolder tasviewHolder, int position) {
        //tasviewHolder.setIsRecyclable(false);
        tasviewHolder.bind(position);
    }

    @Override
    public int getItemCount() {
        return tasList.size();
    }

    class TasviewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        TextView name, roll;
        CheckBox checkBox;
        public TasviewHolder(@NonNull View itemView) {
            super(itemView);
            name =itemView.findViewById(R.id.tasName);
            roll =itemView.findViewById(R.id.tasRoll);
            checkBox =itemView.findViewById(R.id.tasBox);
            itemView.setOnClickListener(this);
        }
        void bind(int position) {

            if (!itemStateArray.get(position, false)) {
                checkBox.setChecked(false);}
            else {
                checkBox.setChecked(true);
            }
            checkBox.setText(String.valueOf(tasList.get(position).getPosition()));
            TasModel tasModel =tasList.get(position);
            name.setText(tasModel.getName());
            roll.setText(tasModel.getRoll());
        }

        @Override
        public void onClick(View v) {
            int adapterPosition = getAdapterPosition();
            if (tasList.get(adapterPosition).getChecked()) {
                checkBox.setChecked(false);
                tasList.get(adapterPosition).setChecked(false);
            }
            else {
                checkBox.setChecked(true);
                tasList.get(adapterPosition).setChecked(true);
            }
        }
    }

}

CodePudding user response:

You have missed this line inside bind()

name.setOnClickListener(this);

or

roll.setOnClickListener(this);

You have implemented the OnClickListener and never assigned to which view that OnClickListener belongs.

If you are trying to check the checkbox when user taps on checkbox then you should use:

satView.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override 
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
      if (buttonView.isChecked()) { 
            // checked
      } 
      else 
      {
            // not checked
      }
   }

});

and for to retain the state so checkbox is still checked even after scrolling.

 void bind(int position) {
        
        // You are missing this line 
        tasPrst.setChecked(tasList.get(position).getChecked());
        
        // rest of your code.
       //...
     
    }

You do not need the sparseArray you are already maintaining the state of your checkbox in

setChecked & getChecked function

This is because your ViewHolder is recycled when scrolling. So, you already have the state you are just not setting it correctly.

I have tested this code with over 2000 items so it works just fine.

  • Related