带有单选按钮的 Android 双向数据绑定
Posted
技术标签:
【中文标题】带有单选按钮的 Android 双向数据绑定【英文标题】:Android two-way databinding with radio button 【发布时间】:2017-02-28 22:52:34 【问题描述】:我正在尝试以两种方式将模型的布尔值绑定到 RadioButton。 但是,代码无法编译。我有以下错误消息:
错误:(41, 21) 错误: 变量 receiveDataContainer 已定义 在 onChange() 方法中
Error:(45, 59) 错误:不兼容的类型:布尔值无法转换 到 ReceiveDataContainer
Error:(46, 17) 错误:不兼容的类型:ReceiveDataContainer 不能 转换为布尔值
我的班级:
public class ReceiveDataContainer extends BaseObservable
private boolean isShopReceiveMethodChosen;
@Bindable
public boolean isShopReceiveMethodChosen()
return isShopReceiveMethodChosen;
public void setShopReceiveMethodChosen(boolean isShopReceiveMethodChosen)
this.isShopReceiveMethodChosen = isShopReceiveMethodChosen;
notifyPropertyChanged(BR.shopReceiveMethodChosen);
XML:
<RadioGroup
android:layout_
android:layout_
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:orientation="vertical"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/inputLayoutPhoneNumber">
<RadioButton
android:id="@+id/radio_receive_in_store"
android:text="@string/radio_button_receive_in_store"
android:checked="@=receiveDataContainer.shopReceiveMethodChosen"
style="@style/CartReceptionRadioButton" />
<RadioButton
android:id="@+id/radio_send_to_home"
android:text="@string/radio_button_send_to_home"
style="@style/CartReceptionRadioButton" />
</RadioGroup>
并且生成的代码被标记为无效:
// Inverse Binding Event Handlers
private android.databinding.InverseBindingListener radioReceiveInStorea = new android.databinding.InverseBindingListener()
@Override
public void onChange()
// Inverse of receiveDataContainer.shopReceiveMethodChosen
// is receiveDataContainer.setShopReceiveMethodChosen((boolean) callbackArg_0)
boolean callbackArg_0 = radioReceiveInStore.isChecked();
// localize variables for thread safety
// receiveDataContainer.shopReceiveMethodChosen
boolean shopReceiveMethodCho = false;
// receiveDataContainer
com.abastra.home_cook.catalogue.data.models.ReceiveDataContainer receiveDataContainer = mReceiveDataContaine;
// receiveDataContainer != null
boolean receiveDataContainer = false;
receiveDataContainer = (receiveDataContainer) != (null);
if (receiveDataContainer)
receiveDataContainer.setShopReceiveMethodChosen((boolean) (callbackArg_0));
;
值得一提的是,当我删除“=”符号时,代码会编译。
请帮帮我...
【问题讨论】:
【参考方案1】:好的,我通过将我的班级名称从 ReceiveDataContainer 更改为 ReceiveContainer 解决了这个问题。我不知道为什么会这样,但它确实......
更多信息在这里:https://code.google.com/p/android/issues/detail?id=212492
【讨论】:
这取决于布局中变量名的长度。我有一些长名字并遇到了这个错误。缩短名称有效。看起来它也已在 AS 2.3 中修复以上是关于带有单选按钮的 Android 双向数据绑定的主要内容,如果未能解决你的问题,请参考以下文章