Create Different Inversebindingadapter For Short And Integer Values On Android:text Of Edittext
I create these methods for custom data binding @BindingAdapter('android:text') public static void setShortText(TextView view, short value) { view.setText(String.valueOf(value))
Solution 1:
I find a solution, but really not satisfy myself.
I change getIntText
return value from Integer
to int
and problem is solved.
if someone find other better solution please let me know.
@InverseBindingAdapter(attribute = "android:text")
publicstatic int getIntText(TextView view) {
try {
returnInteger.parseInt(view.getText().toString());
} catch (NumberFormatException e) {
return -1;
}
}
Solution 2:
For this solution to work correctly, it is necessary to make it possible to receive a null value, and check if the new value of TextView
is different from the previous value.
If I contract, the Binding methods will be executed forever.
Here is my solution:
@BindingAdapter("android:text")funsetBindShortText(view: TextView, value: Short?) {
value?.let {
if (!it.toString().contentEquals(view.text))
view.text = it.toString()
}
}
@InverseBindingAdapter(attribute = "android:text")fungetBindShortText(view: TextView): Short? {
returntry {
view.text.toString().toShort()
} catch (e: java.lang.NumberFormatException) {
null
}
}
@BindingAdapter("android:text")funsetBindIntText(view: TextView, value: Int?) {
value?.let {
if (!it.toString().contentEquals(view.text))
view.text = it.toString()
}
}
@InverseBindingAdapter(attribute = "android:text")fungetBindIntText(view: TextView): Int? {
returntry {
view.text.toString().toInt()
} catch (e: NumberFormatException) {
null
}
}
Post a Comment for "Create Different Inversebindingadapter For Short And Integer Values On Android:text Of Edittext"