Safeunbox() Cannot Be Inverted
Solution 1:
I haven't worked with Android Architecture Components or with the Data Binding libraries in this particular way, but I think I can still help.
Within your XML, you've got this:
android:checked="@={viewModel.value}"
The system is giving you a warning because it wants you to know that in the case where viewModel.value
is null
, it's going to do something special (behave as though it were false
instead, presumably). It does this via the safeUnbox()
method.
To solve the warning, it's suggesting making the safeUnbox()
call explicit. You can't do that because there's no "inverse" of safeUnbox()
to go back from boolean
to Boolean
.
But it doesn't sound like you have to use safeUnbox()
; you could create your own method that converts Boolean
to boolean
, and then you could use the suggested annotation to declare which method will convert back from boolean
to Boolean
.
publicclassMyConversions {
@InverseMethod("myBox")
publicstaticbooleanmyUnbox(Boolean b) {
return (b != null) && b.booleanValue();
}
publicstaticBooleanmyBox(boolean b) {
return b ? Boolean.TRUE : Boolean.FALSE;
}
}
Now you can change your XML to:
android:checked="@={com.example.stackoverflow.MyConversions.myUnbox(viewModel.value)}"
I hope this helps. If it turns out that I'm way off-base, let me know; I'd love to learn more about this topic.
Most of what I have in this answer I learned from https://medium.com/google-developers/android-data-binding-inverse-functions-95aab4b11873
Solution 2:
Came across this issue and found an easier solution. You can avoid this warning by creating a custom BindingAdapter for the boxed type like this:
@BindingAdapter("android:checked")
publicstaticvoidsetChecked(CompoundButton checkableView, Boolean isChecked) {
checkableView.setChecked(isChecked != null ? isChecked : false);
}
This solution can be replicated to any property like visibility
, enabled
etc. and to any boxed primitive like Integer
, Float
etc.
You can also provide the value to be used in case your LiveData
value is null
like this:
@BindingAdapter(value={"android:checked", "nullValue"}, requireAll=false)
publicstaticvoidsetChecked(CompoundButton checkableView, Boolean isChecked, boolean nullValue) {
checkableView.setChecked(isChecked != null ? isChecked : nullValue);
}
And call it like this:
<CheckBox...android:checked='@{viewModel.value}'app:nullValue="@{false}"
/>
Solution 3:
do like this in xml. Last line of code is the most important.
<layout><data><importtype="android.view.View"/><variablename="entryViewModel"type="com.craiovadata.groupmap.viewmodel.EntryViewModel" /></data><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:visibility="@{safeUnbox(entryViewModel.showBtnMyGroups) ? View.VISIBLE : View.GONE, default = gone}" />
Solution 4:
I find a code ,this this safeUnbox implementation。
/** @hide */protectedstaticintsafeUnbox(java.lang.Integer boxed) {
returnboxed== null ? 0 : (int)boxed;
}
/** @hide */protectedstaticlongsafeUnbox(java.lang.Long boxed) {
returnboxed== null ? 0L : (long)boxed;
}
/** @hide */protectedstaticshortsafeUnbox(java.lang.Short boxed) {
returnboxed== null ? 0 : (short)boxed;
}
/** @hide */protectedstaticbytesafeUnbox(java.lang.Byte boxed) {
returnboxed== null ? 0 : (byte)boxed;
}
/** @hide */protectedstaticcharsafeUnbox(java.lang.Character boxed) {
returnboxed== null ? '\u0000' : (char)boxed;
}
/** @hide */protectedstaticdoublesafeUnbox(java.lang.Double boxed) {
returnboxed== null ? 0.0 : (double)boxed;
}
/** @hide */protectedstaticfloatsafeUnbox(java.lang.Float boxed) {
returnboxed== null ? 0f : (float)boxed;
}
/** @hide */protectedstaticbooleansafeUnbox(java.lang.Boolean boxed) {
returnboxed== null ? false : (boolean)boxed;
}
so ,if you value is not base type,you can not direct use safeUnbox,you should define a static function to safe unbox by youself.
Post a Comment for "Safeunbox() Cannot Be Inverted"