lundi 30 novembre 2020

Update sqlite database from recycler view adapter on checkbox state change

I want to update my SQLite database on checkbox state changes inside recycler view. All data is listed in recycler view. when i click the checkbox i want to call a databaase function to update the database

public class OverTimeAdapter extends RecyclerView.Adapter<OverTimeAdapter.MyViewHolder> {
    private Context context;
    private ArrayList otId,otDate,otShift,paymentStatus;
    private DatabaseHelper attendanceDB;

    OverTimeAdapter(Context context,ArrayList otId,ArrayList otDate,ArrayList otShift ,ArrayList paymentStatus){
        this.context =context;
        this.otId = otId;
        this.otDate = otDate;
        this.otShift=otShift;
        this.paymentStatus = paymentStatus;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
      
        if (paymentStatus.get(position)=="1"){
            holder.otListPayment.setChecked(true);
        }else {
            holder.otListPayment.setChecked(false);
        }
        OtDbId =String.valueOf(otId.get(position));
        holder.otListPayment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean checked =holder.otListPayment.isChecked();
                if (checked){
                    payStatus=1;
                }else{
                    payStatus=0;
                }

                // call db update function from here
                // attendanceDB = new DatabaseHelper(this); This is error
                //attendanceDB.updateOvertime()   /*error*/
             
                
            }
        });
    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder{
        public CheckBox otListPayment;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            otListPayment =(CheckBox) itemView.findViewById(R.id.chk_ot_list_payment);

        }
    }
}

How can we initialize the DBHelper object and call the function? and from where(inside which function) i can do it?




Aucun commentaire:

Enregistrer un commentaire