简体   繁体   中英

Android Data Binding Issue: The expression XXX cannot be inverted

I'm a newbie for android data binding. I want to bind multiple SeekBars to a collection of float, such as

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:

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:

<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://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 :

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. After add it to build.gradle, all works fine.

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.

Thanks everyone who give my suggestion. I think I just ask a silly question.

Refer to: Databinding annotation processor kapt warning

Solution

Ok, I found the problem: My project does not use Kapt. After add it to build.gradle, all works fine.

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.

Thanks everyone who give my suggestion. I think I just ask a silly question.

Refer to: Databinding annotation processor kapt warning

In my case the problem was that I was trying to make a custom TwoWay DataBinding Adapter

but my field in the ViewModel class was LiveData instead of MutableLiveData

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM