0

Is there a way I can set a RadioButton with a string value in my Firebase Realtime Database? Is it possible? Below is my code.

DatabaseReference questions_beg_java = FirebaseDatabase.getInstance().getReference().child("Questions").child("BeginnerJava").child("QuestionOne");
        questions_beg_java.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                String question_one = dataSnapshot.child("question_one").getValue().toString();
                String answer_one = dataSnapshot.child("answer_one").getValue().toString();
                String answer_two = dataSnapshot.child("answer_two").getValue().toString();
                String answer_three = dataSnapshot.child("answer_three").getValue().toString();

                //radioButton1.
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

What do I add to set a value to the RadioButton? As in retrieving it from Firebase Realtime Database?

Grimthorr
  • 6,856
  • 5
  • 41
  • 53
  • How do you want to set the RadioButton? Which value from the database should be used? Could you use a [switch statement](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html)? – Grimthorr Nov 15 '18 at 15:03
  • I am having a string value in my database And I want to give the radiobutton the same name as the string value. – Perfect Updates Nov 15 '18 at 15:12
  • The Android [`RadioButton`](https://developer.android.com/reference/android/widget/RadioButton) view inherits from [`TextView`](https://developer.android.com/reference/android/widget/TextView), so you can use [`setText()`](https://developer.android.com/reference/android/widget/TextView#setText(java.lang.CharSequence)). – Grimthorr Nov 15 '18 at 15:22
  • This is shameful. Thanks a lot bro – Perfect Updates Nov 15 '18 at 15:24
  • Glad I could help, I'll add it as an answer for you. – Grimthorr Nov 15 '18 at 15:43

1 Answers1

1

The Android RadioButton widget is basically a TextView with some extra styling and functionality. It is therefore inheriting all methods from TextView, including setText().

This means that you can do something like:

String answer_one = dataSnapshot.child("answer_one").getValue().toString();
radioButton1.setText(answer_one);
Grimthorr
  • 6,856
  • 5
  • 41
  • 53