最近我在android上发现了一种叫做数据绑定的东西,这看起来非常有趣,因此我继续在我将要开始的项目之一中实现它。当我在textview中进行数据绑定时,一切都会按预期进行,但如果尝试在edittext中进行,则编译器将生成错误。在那之后,我开始按照YouTube上的教程进行教学,这对我来说似乎很好,但是尽管我按照他说的所有步骤进行操作,但并没有成功。
有人可以帮我吗?
xml
<?xml version="1.0" encoding="utf-8"?>
<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>
<variable
name="student2"
type="com.juan97.prueba.ViewModel.Student2ViewModel"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_marginTop="50sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="@{student2.hint1}"/>
<TextView
android:layout_marginTop="50sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="@{student2.hint2}"/>
</LinearLayout>
</layout>
视图
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding activityMainBinding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
Student2ViewModel viewModel = new Student2ViewModel(new Student2("usuario", "contrasena"));
activityMainBinding.setStudent(viewModel);
}
}
ViewModel
public class Student2ViewModel {
private String email;
private String password;
private String hint1;
private String hint2;
public Student2ViewModel(Student2 student2) {
this.hint1 = student2.hint1;
this.hint2 = student2.hint2;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getHint1() {
return hint1;
}
public void setHint1(String hint1) {
this.hint1 = hint1;
}
public String getHint2() {
return hint2;
}
public void setHint2(String hint2) {
this.hint2 = hint2;
}
}
型号
public class Student2 {
private String email;
private String password;
public String hint1;
public String hint2;
public Student2(String hint1, String hint2) {
this.hint1 = hint1;
this.hint2 = hint2;
}
public Student2() {
email = null;
password = null;
hint1 = null;
hint2 = null;
}
}
build.gradle模块:app
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.juan97.prueba"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dataBinding { <- this is the library that I need to use dataBinding
enabled = true
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
编译错误
错误:找不到符号类ViewModel <-这就是控制台所说的
自动生成的类给我错误:
import android.databinding.Bindable;
import android.databinding.DataBindingComponent;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.juan97.prueba.ViewModel; <- error here, so everything that has viewModel contains an error
public abstract class ActivityMainBinding extends ViewDataBinding {
@Bindable
protected ViewModel.Student2ViewModel mStudent2; <- error here
protected ActivityMainBinding(DataBindingComponent _bindingComponent, View _root,
int _localFieldCount) {
super(_bindingComponent, _root, _localFieldCount);
}
public abstract void setStudent2(@Nullable ViewModel.Student2ViewModel student2); <- error here
@Nullable
public ViewModel.Student2ViewModel getStudent2() { <- error here
return mStudent2;
}
@NonNull
public static ActivityMainBinding inflate(@NonNull LayoutInflater inflater,
@Nullable ViewGroup root, boolean attachToRoot) {
return inflate(inflater, root, attachToRoot, DataBindingUtil.getDefaultComponent());
}
@NonNull
public static ActivityMainBinding inflate(@NonNull LayoutInflater inflater,
@Nullable ViewGroup root, boolean attachToRoot, @Nullable DataBindingComponent component) {
return DataBindingUtil.<ActivityMainBinding>inflate(inflater, com.juan97.prueba.R.layout.activity_main, root, attachToRoot, component);
}
@NonNull
public static ActivityMainBinding inflate(@NonNull LayoutInflater inflater) {
return inflate(inflater, DataBindingUtil.getDefaultComponent());
}
@NonNull
public static ActivityMainBinding inflate(@NonNull LayoutInflater inflater,
@Nullable DataBindingComponent component) {
return DataBindingUtil.<ActivityMainBinding>inflate(inflater, com.juan97.prueba.R.layout.activity_main, null, false, component);
}
public static ActivityMainBinding bind(@NonNull View view) {
return bind(view, DataBindingUtil.getDefaultComponent());
}
public static ActivityMainBinding bind(@NonNull View view,
@Nullable DataBindingComponent component) {
return (ActivityMainBinding)bind(component, view, com.juan97.prueba.R.layout.activity_main);
}
}
(更新)
在我的项目中,由于我希望应用MVVM模式,所以我有一个用于模型的程序包,一个用于视图的程序包和一个用于viewModel的程序包。但是最近我发现,如果将所有类放在上述包之外的主文件夹中,则编译器将停止发送错误,并且应用程序将正确编译。那么,为什么会发生这种情况?如何在不产生编译器发送的错误的情况下继续使用上述软件包中的类呢?
谢谢!
答案 0 :(得分:0)
几件事:
1)您的xml必须以以下开头和结尾:
<layout>
...
</layout >
2)您需要在模块的build.gradle中添加以下行:
android {
....
dataBinding {
enabled = true
}
}