2015-05-12 57 views
0

我正在关注的Qiitanium应用程序的代码(见链接高亮行)绑定单选按钮和我有麻烦搞清楚怎么可以绑定单选按钮如何使用RxJava

说我有一个R A RadioGroup中.id.rgMyButtons作为Id,它包含3个RadioButtons“Dead”,“Alive”,“Body Missing”和Ids是R.id.rbDead,R.id.rbAlive,R.id.rbMissing

我设置了RadioGroup字段为

Rx<RadioGroup> radioGroup; 

我得到在onViewCreated中设置的视图为

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

在onBind中,我想将RadioGroup绑定到模型数据,以便将地图直接返回到该组中正确的RadioButton。我正在寻找像

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

因此,它绑定到RadioGroup并自动设置正确的值。

person_state()实际上返回2为Dead,3为Alive,4为Missing,我希望检查RadioGroup中的正确字段。我如何在RxAndroid中实现这一点?

回答

1

我能够自己解决这个问题。这里的人谁可能会遇到同样的问题,答案....

我在课堂上写了一个功能

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; 

      } 
     } 
    }; 
} 

而且里面的onBind我用

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