I am trying to use addSnapshotListener to listen to the changes of radiobutton in Firestore, which works with the string. I simply want to categorize better by grouping the response with a List. However, I found that there is not a method for snapshot.getList() and therefore I tried with
List<String> participate = snapshot.getList("Would you like to participate"); which doesn't work. I wonder how would I group my radiobutton response within a list to better organize the data in Firestore? Thank you in advance!
Below is the code I am working with for two radiobutton
Global:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
FirebaseFirestore db = FirebaseFirestore.getInstance();
DocumentReference uidRef = db.collection("users").document(uid);
RadioGroup radioGroup;
RadioButton yesButton;
RadioButton noButton;
For the OnCreate,
yesButton = findViewById(R.id.a1);
noButton = findViewById(R.id.a2);
radioGroup = findViewById(R.id.radioGroup);
String yes = yesButton.getText().toString();
String no = yesButton.getText().toString();
addSnapshotListener to listen the changes
uidRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot snapshot, @Nullable FirebaseFirestoreException e) {
if (e != null) {
Log.w(TAG, "Listen failed.", e);
return;
}
if (snapshot != null && snapshot.exists()) {
String participate = snapshot.getString("Would you like to participate");
if (participate != null) {
yesButton.setChecked(true);
} else {
noButton.setChecked(true);
}
}
}
});
The two radio buttons:
yesButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (yesButton.isChecked()) {
Map<String, Object> update = new HashMap<>();
update.put("Would you like to participate", yes);
uidRef.update(update).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(getApplicationContext(), "Response updated ", Toast.LENGTH_SHORT).show();
}
});
}
}
});
noButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (noButton.isChecked()) {
Map<String, Object> update = new HashMap<>();
update.put("Would you like to participate", no);
uidRef.update(update).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(getApplicationContext(), "Response updated ", Toast.LENGTH_SHORT).show();
}
});
}
}
});
