I've created an adapter with an OnItemClickListener and set it to a check box. I've succeeded in making the check box toggle a Boolean value in Firebase firestore but it takes 2 clicks to change the value. When clicking the check box it doesn't toggle to checked until the second click. The same goes for unchecking.
The method is called in onCreate.
"done" is the Boolean field in my database.
The check box is on a recycler view nested in another recycler view. This is why it has a nested collection/document and a variable for an ID. I excluded the intents used to create the variables.
private void setUpCheckBox() {
adapter.setOnItemClickListener(new ToDoAdapter.OnItemClickListener() {
@Override
public void onItemClick(DocumentSnapshot documentSnapshot, final int
position) {
final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox_complete);
Map<String, Object> done = new HashMap<>();
if (checkBox.isChecked()) {
checkBox.setChecked(true);
done.put("done", true);
}
if (!checkBox.isChecked()) {
checkBox.setChecked(false);
done.put("done", false);
}
db.collection("customer2").document(id1).collection("To Do").document(id2)
.set(done, SetOptions.merge())
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(CustomerProfile.this, id2, Toast.LENGTH_SHORT).show();
}
});
}
});
}
This is a nested Class in my adapter Class.
private OnItemClickListener listener;
listener is declared in parent class.
class ToDoHolder extends RecyclerView.ViewHolder {
CheckBox checkBoxComplete;
public ToDoHolder(@NonNull View itemView) {
super(itemView);
checkBoxComplete = itemView.findViewById(R.id.checkBox_complete);
checkBoxComplete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION && listener != null) {
listener.onItemClick(getSnapshots().getSnapshot(position), position);
}
}
});
}
}
public interface OnItemClickListener {
void onItemClick(DocumentSnapshot documentSnapshot, int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
this.listener = listener;
}
}
Aucun commentaire:
Enregistrer un commentaire