Saifur Rahman Mohsin
Saifur Rahman Mohsin

Reputation: 1011

How to bind Radio Buttons using RxJava

I'm following the code of the Qiitanium app (See the highlighted lines in link) and I have trouble figuring out how I can bind RadioButtons

Say I have a RadioGroup with R.id.rgMyButtons as Id and it contains 3 RadioButtons "Dead", "Alive", "Body Missing" with Ids are R.id.rbDead, R.id.rbAlive, R.id.rbMissing

I set the RadioGroup field as

Rx<RadioGroup> radioGroup;

I get the view set in onViewCreated as

rdGroup = RxView.findById(this, R.id.rgMyButtons);
rdGroup.get().setOnCheckedChangeListener(mOnPersonStateUpdateListener);

In onBind, I'd like to bind the RadioGroup to the model data so that the value which returns maps directly to the correct RadioButton in that group. I'm looking for something like

rdGroup.bind(person.state(), RxActions.someAction()),

So that it is bound to the RadioGroup and automatically sets the correct value.

The person_state() actually returns 2 for Dead, 3 for Alive and 4 for Missing and I want that to check to the correct field in RadioGroup. How can I achieve this in RxAndroid?

Upvotes: 0

Views: 1359

Answers (1)

Saifur Rahman Mohsin
Saifur Rahman Mohsin

Reputation: 1011

I was able to solve this on my own. Here's the answer for anyone who might come across the same issue....

I wrote a function in the class as

protected RxAction<RadioGroup, String> setRadioButton() {
    return new RxAction<RadioGroup, String>() {
        @Override
        public void call(final RadioGroup radioGroup, final String selection) {
           RadioButton rb;
           switch(selection)
           {
               case "2":
                   rb = (RadioButton)findViewById(R.id.rbDead);
                   rb.setChecked(true);
                   break;

               case "3":
                   rb = (RadioButton)findViewById(R.id.rbAlive);
                   rb.setChecked(true);
                   break;

               case "4":
                   rb = (RadioButton)findViewById(R.id.rbMissing);
                   rb.setChecked(true);
                   break;

           }
        }
    };
}

And inside the onBind I used

rdGroup.bind(person.state(), setRadioButton()),

Upvotes: 1

Related Questions