[英]Android Data Binding Issue: The expression XXX cannot be inverted
I'm a newbie for android data binding.我是android数据绑定的新手。 I want to bind multiple SeekBars to a collection of float, such as我想将多个 SeekBars 绑定到一个浮点数集合,例如
SeekBar1.progress <---> modelArray[0]
SeekBar2.progress <---> modelArray[1]
...
Since the progress of SeekBar is a Int type, I think it would be better to use Converter, and below is the converter code:由于SeekBar的进度是Int类型,我觉得用Converter会更好,下面是转换器代码:
import android.databinding.InverseMethod
import android.util.Log
import kotlin.math.roundToInt
class Converter {
@InverseMethod("progressToFloat")
fun floatToProgress(value: Float): Int {
val result = (value * 100.0f).roundToInt()
Log.i("MODEL", "convert $value to $result(Int)")
return result
}
fun progressToFloat(value: Int): Float {
return value.toFloat()/100.0f
}
}
and the model structure looks like:模型结构如下所示:
class Model {
val params by lazy {
ObservableArrayMap<Int, Float>()
}
//....
}
and my xml is following:我的xml如下:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data class="MyBinding">
<variable name="Converter" type="com.mypackage.model.Converter"></variable>
<variable
name="Converter"
type="com.mypackage.model.Converter"></variable>
<variable
name="model"
type="com.mypackage.model.Model"></variable>
</data>
...
<SeekBar
android:id="@+id/seekBar"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:progress="@={Converter.floatToProgress(model.params[0])}"/>
The problem is, every time I build it, it shows :问题是,每次我构建它时,它都会显示:
Found data binding errors.
****/ data binding error ****
msg:The expression converter.floatToProgress(modelParams0) cannot be inverted:
There is no inverse for method floatToProgress, you must add an @InverseMethod
annotation to the method to indicate which method should be used when using
it in two-way binding expressions
I already refer to lots of website, includes following:我已经参考了很多网站,包括以下内容:
https://medium.com/androiddevelopers/android-data-binding-inverse-functions-95aab4b11873 https://medium.com/androiddevelopers/android-data-binding-inverse-functions-95aab4b11873
https://developer.android.com/topic/libraries/data-binding/two-way https://developer.android.com/topic/libraries/data-binding/two-way
But I still cannot find out what problem is.但我仍然无法找出问题所在。 Could anyone give me some suggestion?谁能给我一些建议?
My development environment我的开发环境
macOS High Sierra,
Android Studio 3.2.1, with compileSdkVersion 28 and gradle 3.2.1
Update: I also try to write Converter as following :更新:我也尝试将 Converter 编写如下:
object Converter { // change to object
@InverseMethod("progressToFloat")
@JvmStatic fun floatToProgress(value: Float): Int { // Add static
val result = (value * 100.0f).roundToInt()
Log.i("MODEL", "convert $value to $result(Int)")
return result
}
@JvmStatic fun progressToFloat(value: Int): Float { // Add static
return value.toFloat()/100.0f
}
}
It still does not work.它仍然不起作用。
Solution Ok, I found the problem: My project does not use Kapt.解决方案好的,我找到了问题:我的项目没有使用Kapt。 After add it to build.gradle, all works fine.将其添加到 build.gradle 后,一切正常。
apply plugin: 'kotlin-kapt'
Also, I update my converter body as following:此外,我更新我的转换器主体如下:
object Converter {
@InverseMethod("progressToFloat")
@JvmStatic fun floatToProgress(value: Float): Int {
val result = (value * 100.0f).roundToInt()
Log.i("MODEL", "convert $value to $result(Int)")
return result
}
@JvmStatic fun progressToFloat(value: Int): Float {
val result = value.toFloat()/100.0f
Log.i("MODEL", "convert $value to $result(Float)")
return result
}
}
If the @JVMStatic removed, the project can compile successfully, but the binding does not work.如果去掉@JVMStatic,项目可以编译成功,但是绑定不起作用。
Thanks everyone who give my suggestion.感谢所有给我建议的人。 I think I just ask a silly question.我想我只是问了一个愚蠢的问题。
Refer to: Databinding annotation processor kapt warning参考: 数据绑定注释处理器 kapt 警告
Ok, I found the problem: My project does not use Kapt.好的,我发现了问题:我的项目没有使用Kapt。 After add it to build.gradle, all works fine.将其添加到 build.gradle 后,一切正常。
apply plugin: 'kotlin-kapt'
Also, I update my converter body as following:此外,我更新我的转换器主体如下:
object Converter {
@InverseMethod("progressToFloat")
@JvmStatic fun floatToProgress(value: Float): Int {
val result = (value * 100.0f).roundToInt()
Log.i("MODEL", "convert $value to $result(Int)")
return result
}
@JvmStatic fun progressToFloat(value: Int): Float {
val result = value.toFloat()/100.0f
Log.i("MODEL", "convert $value to $result(Float)")
return result
}
}
If the @JVMStatic removed, the project can compile successfully, but the binding does not work.如果去掉@JVMStatic,项目可以编译成功,但是绑定不起作用。
Thanks everyone who give my suggestion.感谢所有给我建议的人。 I think I just ask a silly question.我想我只是问了一个愚蠢的问题。
Refer to: Databinding annotation processor kapt warning参考: 数据绑定注释处理器 kapt 警告
In my case the problem was that I was trying to make a custom TwoWay DataBinding Adapter
就我而言,问题是我试图制作一个自定义的TwoWay DataBinding Adapter
but my field in the ViewModel
class was LiveData
instead of MutableLiveData
但是我在ViewModel
类中的字段是LiveData
而不是MutableLiveData
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.