Home > Enterprise >  Listening radio buttons and saving the string results in Firestore java
Listening radio buttons and saving the string results in Firestore java

Time:09-22

I am building a survey app and trying to save and listen to the changes of radioButton and store the response as a String. The text of radioButton represents the result of the user's response. I am currently working on storing two strings from the radio buttons. I will expand the concept as I have some survey questions that have four choices for the user to choose from and I want to store and listen to the corresponding strings as well.

I got help from Alex earlier to store as a Boolean. I tried to follow the suggestion to store the correct response as a string. The problem I have is that the string response yes only saved once and does not listen and update the response when the user select no and go back to yes again.

Below are the codes I am working on:

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 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 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();
                                }
                            });
                        }
                    }
                });
        

Firestore structure:

Firestore-root
  |
  --- users
       |
       --- userid
            |
            --- Would you like to participate: yes (//this string does not update)

Added Firestore screenshot (response stored once under the user UID): enter image description here

CodePudding user response:

The problem I have is that the string response yes only saved once and does not listen and update the response when the user select no and go back to yes again.

You're getting this behavior because you're using the text of the same button:

String yes = yesButton.getText().toString();
String no = yesButton.getText().toString();

See? You're using the yesButton twice. To solve this, change the second line to:

String no = noButton.getText().toString();
//                        
  • Related