Search code examples
androidkotlinbindinggetter-setter

A Kotlin property is in looping when getting the value ($field)


When I bind the value in the 'name' property in xml, the getter seems to be in a loop and the value inside it, is concatenating in the screen until I stop the app.

1 - I don't know with sure yet if I need to use notifyPropertyChanged() or the anotations @set and @get;

2 - If I set the get without the concatenating string, it's works nicelly: get() = field;

3 - If I try to return the get value inside braces, the problem keeps to occour: get(){return "Field: $field"};

This is the model:

class ContactModel : BaseObservable(){

    @set:Bindable
    @get:Bindable
    var name: String = ""
        get() = "Field: $field"
        set(value) {
            field = value
            notifyPropertyChanged(BR.name)
        }


    @set:Bindable
    @get:Bindable
    var email: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.email)
        }

    @set:Bindable
    @get:Bindable
    var phone: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.phone)
        }

}

Here's the activity:

class MainActivity : AppCompatActivity() {


    lateinit var binding: ActivityMainBinding
    var contactModel: ContactModel = ContactModel()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        contactModel = ContactModel(/*"Rômulo", "romulocoviel@gmail.com", "(19):98421-0821"*/)
        contactModel.name = "Rômulo"
        contactModel.email = "romulocoviel@gmail.com"
        contactModel.phone = "(19):98421-0821"

        binding.contactModel = contactModel
        binding.setLifecycleOwner(this)
    }

    fun changeSignatures(view: View) {
        Log.e("TESTING", "Testando!" + contactModel.name)
        val nameList: ArrayList<ContactModel> = ArrayList()

        contactModel.name = "asdasd"
        contactModel.email = "asdasda"
        contactModel.phone = "asdasd"

    }

}

And here's the XML that I have a button that changes the values when tapped and the binding views:

<data>
    <variable
            name="contactModel"
            type="com.example.romulo.bindingmetricsconversor.ContactModel"/>
</data>

<android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <TextView
            android:text="@={contactModel.name}"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvName" android:layout_marginTop="8dp"
            app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
            android:layout_marginLeft="8dp" android:layout_marginStart="8dp"/>
    <TextView
            android:text="@={contactModel.email}"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvEmail" android:layout_marginTop="8dp"
            app:layout_constraintTop_toBottomOf="@+id/tvName" app:layout_constraintStart_toStartOf="parent"
            android:layout_marginLeft="8dp" android:layout_marginStart="8dp"/>
    <TextView
            android:text="@={contactModel.phone}"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvPhone" android:layout_marginTop="8dp"
            app:layout_constraintTop_toBottomOf="@+id/tvEmail" app:layout_constraintStart_toStartOf="parent"
            android:layout_marginLeft="8dp" android:layout_marginStart="8dp"/>
    <Button
            android:text="Change"
            android:layout_width="wrap_content"
            android:layout_height="49dp"
            android:id="@+id/btChange" android:layout_marginTop="8dp"
            app:layout_constraintTop_toTopOf="parent" android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginEnd="8dp" android:layout_marginRight="8dp"
            app:layout_constraintStart_toStartOf="parent" android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp" android:onClick="changeSignatures"/>
</android.support.constraint.ConstraintLayout>

The result in the screen always is:

Field: asdasd

Field:Field: asdasd

Field:Field: asdasd

Field:Field:Field: asdasd

Field:Field:Field:Field: asdasd

... to the infinite


Solution

  • Just for the sake of completeness:

    it seems like whenever the text view is updated by the property change listener it detects a change in its own content and thus tries to save back to the observable, triggering a loop, since you're using two-way binding.

    The problem can be solved by using one-way binding instead (@{}), as upon changing its text the text view would trigger its own listeners and attempt to modify the observable, sending it into an infinite recursion.