How to bind Radio Buttons using RxJava

拥有回忆 提交于 2020-01-06 20:01:48

问题


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?


回答1:


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()),


来源:https://stackoverflow.com/questions/30180429/how-to-bind-radio-buttons-using-rxjava

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!