Errors when trying to set up Data Binding

Pfredd :

I am trying to convert an existing java app to utilize Data Binding. I can't seem to get the reference to my view model recognized in the layout xml file.

I have recreated the problem in this very simple example.

I have enabled data binding in my build.gradle file.

When I try to point the TextView's android:text field to a reference in my View Model, it turns red and the error message says Missing /

Here is my layout file: activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable name="viewmodel" type="com.muddco.btest1.data.VModel"/>
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@(viewmodel.data1)"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

Here is my view model: data/VModel.java:

package com.muddco.btest1.model;

import androidx.lifecycle.ViewModel;

public class VModel  extends ViewModel {
    String data1 = "Testing 1 2 3";
}

And here is MainActivity.java. It has not been modified yet to utilize Data Binding:

package com.muddco.btest1;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
Merthan E :
android:text="@(viewmodel.data1)"

Change that to curly braces like so:

android:text="@{viewmodel.data1}"

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=416946&siteId=1
Recommended