gpt4 book ai didi

java - 如何修复此 DataBinding 和 LiveData 代码?

转载 作者:行者123 更新时间:2023-12-02 09:38:21 26 4
gpt4 key购买 nike

我正在尝试将 DataBinding 与 LiveData 连接,但遇到了一些问题,我不知道如何修复。我不确定 LifecycleOwner 是否做错了什么,但它不起作用。还值得赞赏的是为什么正确的方法是正确的一些解释。该应用程序甚至没有启动,我不知道我的错误是什么。感谢您的帮助

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);
}
}

最佳答案

您的代码通常是正确的。

您需要确保 build.gradle 中有以下行:

android {
...
dataBinding {
enabled = true
}
}
dependencies {
...
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
}

代码的更正版本

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>
</layout>
package com.example.teststuff;

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

public class MainActivityViewModel extends ViewModel {

private MutableLiveData<String> text = new MutableLiveData<>();

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

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

关于java - 如何修复此 DataBinding 和 LiveData 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57316069/

26 4 0