This question already has an answer here:
How can you create a proper recycler view with CheckBox using firebase, currently I can create 5 viewHolders and everythings goes fine, when I create 6 or more viewHolder the checkboxes start checking at random. Has anyone done something similar to this? Any help would be highly appreciated!!
Currently this is my OnBindViewHolder:
public void onBindViewHolder(@NonNull final MyViewHolder holder, int position){
final Habitos habitos = mHabitosList.get(position);
holder.nombre.setText(habitos.getNombre());
FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
mDatabaseReference = mDatabase.getReference("Habitos");
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if(user != null) {
userId = user.getUid();
habitosReference = mDatabaseReference.child(userId);
Focus focus = new Focus();
mDays = focus.getPrevFiveDaysNumbers();
habitosId = mHabitosList.get(position).getHabitosId();
habitosReference.child(habitosId).child("checks").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
mChecks = (HashMap<String, Boolean>) dataSnapshot.getValue();
Log.d("Checky", mChecks+"");
assert mChecks != null;
checkBox1 = mChecks.get(mDays[0]);
checkBox2 = mChecks.get(mDays[1]);
checkBox3 = mChecks.get(mDays[2]);
checkBox4 = mChecks.get(mDays[3]);
checkBox5 = mChecks.get(mDays[4]);
checkListeners(holder, habitos);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
And to listen to the checkboxes I have the following method:
private void checkListeners(MyViewHolder holder, final Habitos habitos) {
holder.CB1.setChecked(checkBox1);
holder.CB1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
String HabitosId = habitos.getHabitosId();
int total = habitos.getTotal();
if (b) {
habitosReference.child(HabitosId).child("checks").child(mDays[0]).setValue(true);
total++;
} else {
habitosReference.child(HabitosId).child("checks").child(mDays[0]).setValue(false);
total--;
}
habitosReference.child(HabitosId).child("total").setValue(total);
}
});
This adds a +1 value everytime a checkbox is checked. Everything is working fine except when I add more than 5 viewHolders. Any idea how could I fix this?
Aucun commentaire:
Enregistrer un commentaire