0

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

2
  • 1
    Please edit your question and add your database structure as a screenshot. Commented Sep 21, 2022 at 7:27
  • Thank you Alex! I just added a screenshot. Commented Sep 21, 2022 at 12:50

1 Answer 1

1

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();
//             👆
Sign up to request clarification or add additional context in comments.

8 Comments

omg. Thanks for pointing this out I did not catch that! I will test again very soon and get back to you!
Ok, make the change and tell me if it works.
It works as expected! Thanks Alex!
Hi sorry Alex. I am trying to expand with 4 choices but I can't get to work with using if and else in snapshot for 4 choices. I used while (participate != null) and the app gets struck. Wondering if you have suggestions/tips on working more than 2 choices
Yep, if, and multiple else if should do the trick.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.