mercredi 7 septembre 2022

Recycler View with Checkbox issue

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

public class Tasadapter extends RecyclerView.Adapter<Tasadapter.TasviewHolder> {

private Context mCtx;
private List<TasModel> tasList;

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 tasPrst;
    String prst;
    public TasviewHolder(@NonNull View itemView) {
        super(itemView);
        name =itemView.findViewById(R.id.tasName);
        roll =itemView.findViewById(R.id.tasRoll);
        tasPrst =itemView.findViewById(R.id.tasBox);
    }
    void bind(int position) {
        tasPrst.setText(String.valueOf(tasList.get(position).getPosition()));

        if (tasList.get(position).getChecked()) {
            tasPrst.setChecked(true);
        }
        else {
            tasPrst.setChecked(false);
        }
        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()) {
            tasPrst.setChecked(false);
            tasList.get(adapterPosition).setChecked(false);
        }
        else {
            tasPrst.setChecked(true);
            tasList.get(adapterPosition).setChecked(true);
        }
    }
}

}




Aucun commentaire:

Enregistrer un commentaire