How can I fix this DataBinding and LiveData code?

I am trying to connect DataBinding with LiveData, but run into some problems, that i dont know how to fix. I am not sure if i did something wrong with the LifecycleOwner, but its not working. Also some explanation why the correct way is right is aprecciated. The app aint even starting and i dont know what my mistake is. Thanks for the help

package com.example.teststuff;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.Observable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.teststuff.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    ActivityMainBinding binding;
    MainActivityViewModel model;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
        model = ViewModelProviders.of(this).get(MainActivityViewModel.class);
        binding.setViewmodel(model);
        model.setText("Hello");
        binding.setLifecycleOwner(this);

    }
}
<?xml version="1.0" encoding="utf-8"?>
<layout>

    <data>
        <variable name="viewmodel" type="com.example.teststuff.MainActivityViewModel" />
    </data>

    <RelativeLayout 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"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <EditText
            android:text="@{viewmodel.text}"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </RelativeLayout>
</layout>
package com.example.teststuff;

import android.widget.Button;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

public class MainActivityViewModel extends ViewModel {

    private MutableLiveData<String> text;

    public MutableLiveData<String> getText() {
        return text;
    }

    public void setText(String text) {
        this.text.setValue(text);
    }
}

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

Your code is generally correct. You need to make sure that there are lines in your build.gradle: android { ... dataBinding { enabled = true } } dependencies { ... implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0' } Corrected version of your code package com.example.teststuff; import androidx.appcompat.app.AppCompatActivity; import androidx.databinding.DataBindingUtil; import androidx.lifecycle.ViewModelProviders; import android.os.Bundle; import com.example.teststuff.databinding.ActivityMainBinding; public class MainActivity extends AppCompatActivity { ActivityMainBinding binding; MainActivityViewModel model; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = DataBindingUtil.setContentView(this,R.layout.activity_main); model = ViewModelProviders.of(this).get(MainActivityViewModel.class); binding.setViewmodel(model); model.setText("Hello"); binding.setLifecycleOwner(this); } } <?xml version="1.0" encoding="utf-8"?> <layout> <data> <variable name="viewmodel" type="com.example.teststuff.MainActivityViewModel" /> </data> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <EditText android:text="@{viewmodel.text}" android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout>… Read more »