mardi 6 octobre 2020

how to have single choice on checkbox in recyclerview?

I have a recyclerview that has a checkbox inside each item and I want the user to be able to check only one item.

The solution I tested does not pick up the check.

I saw many solutions that the one I tested had a higher score.

It has to do this automatically.

What should I do?

I don't want to use RadioGroup

Thanks in advance.

DeveloperAdapter codes:


public class DeveloperAdapter extends RecyclerView.Adapter<DeveloperAdapter.ViewHolder> {

    private List<DeveloperEntity> developerEntities;
    private Context context;
    ArrayList<Boolean> positionArray;



    public DeveloperAdapter(List<DeveloperEntity> developerEntities, Context context) {
        this.developerEntities = developerEntities;
        this.context = context;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.developer_adapter, parent, false);
        return new ViewHolder(view);
    }


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

        final DeveloperEntity developerEntity = developerEntities.get(position);
        holder.mTxtName.setText(developerEntity.getName());
        holder.mTxtPosition.setText(developerEntity.getPosition());

        Drawable drawable = new
                BitmapDrawable(Utility.byteArrayToBitmap(developerEntity.getImg()));
        holder.mImgPhoto.setImageDrawable(drawable);
        holder.mImgPhoto.setImageBitmap(BitmapFactory.decodeByteArray(developerEntity.getImg(), 0, developerEntity.getImg().length));

        setEditRemove(holder, position, developerEntity);

        try {
            developerEntities=DatabaseManager.getDatabaseHelper(context).getDeveloperDao().queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        positionArray = new ArrayList<Boolean>(developerEntities.size());
        for(int i =0;i<developerEntities.size();i++){
            positionArray.add(false);
        }
    }

    private void setEditRemove(final ViewHolder holder, final int position, final DeveloperEntity developerEntity) {
        holder.mImgRemove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog alertDialog = new AlertDialog.Builder(context)
                        .setTitle("Delete User")
                        .setMessage("Are you sure to delete this user?")
                        .setPositiveButton("Delete", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                presentAlertDelete(holder.getAdapterPosition(), developerEntity.getId());
                            }
                        })
                        .setNegativeButton("No", null)
                        .create();
                alertDialog.show();

            }
        });
        holder.mImgEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, AddDeveloperActivity.class);
                intent.putExtra("id", developerEntity.getId());
                context.startActivity(intent);

            }
        });

        holder.chkIsChecked.setOnCheckedChangeListener(null);
        if (positionArray!= null){
            holder.chkIsChecked.setChecked(positionArray.get(position));

        }


        holder.chkIsChecked.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked ){
                    positionArray.set(position, true);

                }else
                    positionArray.set(position, false);
            }


        });
        Log.i("",""+positionArray);






    }

    private void presentAlertDelete(int adapterPosition, long id) {
        try {
            DatabaseManager.getDatabaseHelper(context).getDeveloperDao().deleteById(id);
            developerEntities.remove(adapterPosition);
            notifyItemRemoved(adapterPosition);
            notifyItemChanged(adapterPosition, developerEntities.size());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void deleteUser(int adapterPosition) {
        developerEntities.remove(adapterPosition);
        notifyItemRemoved(adapterPosition);
        notifyItemChanged(adapterPosition, developerEntities.size());
    }


    @Override
    public int getItemCount() {
        return developerEntities == null ? 0 : developerEntities.size();
        //  return  userItemDeveloperDBS.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private CircleImageView mImgPhoto;
        private TextView mTxtName;
        private TextView mTxtPosition;
        private ImageView mImgEdit;
        private ImageView mImgRemove;
        private CheckBox chkIsChecked;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            initView(itemView);
        }

        private void initView(@NonNull final View itemView) {
            mImgPhoto = (CircleImageView) itemView.findViewById(R.id.imgPhoto);
            mTxtName = (TextView) itemView.findViewById(R.id.txtName);
            mTxtPosition = (TextView) itemView.findViewById(R.id.txtPosition);
            mImgEdit = (ImageView) itemView.findViewById(R.id.imgEdit);
            mImgRemove = (ImageView) itemView.findViewById(R.id.imgRemove);
            chkIsChecked = itemView.findViewById(R.id.chkIsChecked);
        }
    }
}



Aucun commentaire:

Enregistrer un commentaire