Java 将布尔值绑定到MVVM复选框
我试图通过viewmodel将布尔值绑定到视图,但是Android Studio抛出了一个错误,我找不到问题。viewModel.inflType是一个布尔值,android:checked也应该是一个布尔值 错误:“@{viewModel.infiltype}”与属性android:checked(attr)boolean不兼容。 消息{kind=ERROR,text=ERROR:“@{viewModel.infiltype}”与属性android:checked(attr)boolean不兼容,sources=[E:\SportveldOnderhoud\app\src\main\res\layou\list\u item\u check.xml:14],原始消息=,工具名=可选的.of(AAPT)}” 我有以下代码(我将粘贴相关的代码片段) Check.java(模型) 视图模型:Java 将布尔值绑定到MVVM复选框,java,android,mvvm,Java,Android,Mvvm,我试图通过viewmodel将布尔值绑定到视图,但是Android Studio抛出了一个错误,我找不到问题。viewModel.inflType是一个布尔值,android:checked也应该是一个布尔值 错误:“@{viewModel.infiltype}”与属性android:checked(attr)boolean不兼容。 消息{kind=ERROR,text=ERROR:“@{viewModel.infiltype}”与属性android:checked(attr)boolean不兼
public class CheckViewModel extends BaseObservable {
private Check mCheck;
private Activity mActivity;
public CheckViewModel(Activity activity) {
mActivity = activity;
}
@Bindable
public String getTitle() {
return mCheck.getTitle();
}
@Bindable
public String getRenderedDate() {
return mCheck.getDate().toString();
}
@Bindable
public boolean infillType() {
return mCheck.isInfilltype();
}
public Check getCheck() {
return mCheck;
}
public void setCheck(Check crime) {
mCheck = crime;
List<String> strings;
notifyChange();
}
public void onCheckClicked() {
Intent intent = CheckPagerActivity.newIntent(mActivity, mCheck.getId());
mActivity.startActivity(intent);
}
}
公共类CheckViewModel扩展了BaseObservable{
私人支票;
私人活动能力;
公共检查视图模型(活动){
活动性=活动性;
}
@装订的
公共字符串getTitle(){
返回mCheck.getTitle();
}
@装订的
公共字符串getRenderDate(){
返回mCheck.getDate().toString();
}
@装订的
公共布尔内嵌类型(){
返回mCheck.isInfilltype();
}
公共检查getCheck(){
返回mCheck;
}
公共无效设置检查(检查犯罪){
mCheck=犯罪;
列出字符串;
notifyChange();
}
勾选()后的公共无效{
Intent-Intent=CheckPagerActivity.newIntent(mActivity,mCheck.getId());
主动性。主动性(意图);
}
}
视图:
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<variable name="viewModel" type="nl.lekolkman.android.sportveldonderhoud.CheckViewModel" />
</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="wrap_content"
android:onClick="@{() -> viewModel.onCheckClicked()}"
>
<CheckBox
android:id="@+id/solved_check_box"
android:checked="@{viewModel.infillType}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/>
<TextView
android:id="@+id/title_text_view"
android:text="@{viewModel.title}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/solved_check_box"
android:textStyle="bold"
tools:text="Crime Title"/>
<TextView
android:id="@+id/date_text_view"
android:text="@{`Date solved: ` + viewModel.renderedDate ?? `(not solved)`}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/solved_check_box"
android:layout_below="@id/title_text_view"
tools:text="Check Date"/>
</RelativeLayout>
</layout>
我对java不是非常熟悉,但我很确定在C#中,您的布尔值需要一个初始值(非null)才能正确绑定 试着改变
private boolean mInfilltype;
到
结果我不得不补充:
android {
...
dataBinding {
enabled = true
}
内建gradle
然而,它仍然不起作用,但在build.gradle文件中将maven添加到存储库后,它确实起作用了您可以使用或扩展Android复合视图绑定适配器
import androidx.databinding.BindingAdapter;
import androidx.databinding.BindingMethod;
import androidx.databinding.BindingMethods;
import androidx.databinding.InverseBindingListener;
import androidx.databinding.InverseBindingMethod;
import androidx.databinding.InverseBindingMethods;
import androidx.annotation.RestrictTo;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
/**
* @hide
*/
@RestrictTo(RestrictTo.Scope.LIBRARY)
@BindingMethods({
@BindingMethod(type = CompoundButton.class, attribute = "android:buttonTint", method = "setButtonTintList"),
@BindingMethod(type = CompoundButton.class, attribute = "android:onCheckedChanged", method = "setOnCheckedChangeListener"),
})
@InverseBindingMethods({
@InverseBindingMethod(type = CompoundButton.class, attribute = "android:checked"),
})
public class CompoundButtonBindingAdapter {
@BindingAdapter("android:checked")
public static void setChecked(CompoundButton view, boolean checked) {
if (view.isChecked() != checked) {
view.setChecked(checked);
}
}
@BindingAdapter(value = {"android:onCheckedChanged", "android:checkedAttrChanged"},
requireAll = false)
public static void setListeners(CompoundButton view, final OnCheckedChangeListener listener,
final InverseBindingListener attrChange) {
if (attrChange == null) {
view.setOnCheckedChangeListener(listener);
} else {
view.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (listener != null) {
listener.onCheckedChanged(buttonView, isChecked);
}
attrChange.onChange();
}
});
}
}
}
Ref:默认情况下,它们被初始化为false,如果您将类型声明为Boolean(object),您可以将其赋值为null,如果它是Boolean(小写b),则它是primitive,并且只能保存false/true。虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接以供参考。如果链接页面发生更改,则仅链接的答案可能无效-
android {
...
dataBinding {
enabled = true
}
import androidx.databinding.BindingAdapter;
import androidx.databinding.BindingMethod;
import androidx.databinding.BindingMethods;
import androidx.databinding.InverseBindingListener;
import androidx.databinding.InverseBindingMethod;
import androidx.databinding.InverseBindingMethods;
import androidx.annotation.RestrictTo;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
/**
* @hide
*/
@RestrictTo(RestrictTo.Scope.LIBRARY)
@BindingMethods({
@BindingMethod(type = CompoundButton.class, attribute = "android:buttonTint", method = "setButtonTintList"),
@BindingMethod(type = CompoundButton.class, attribute = "android:onCheckedChanged", method = "setOnCheckedChangeListener"),
})
@InverseBindingMethods({
@InverseBindingMethod(type = CompoundButton.class, attribute = "android:checked"),
})
public class CompoundButtonBindingAdapter {
@BindingAdapter("android:checked")
public static void setChecked(CompoundButton view, boolean checked) {
if (view.isChecked() != checked) {
view.setChecked(checked);
}
}
@BindingAdapter(value = {"android:onCheckedChanged", "android:checkedAttrChanged"},
requireAll = false)
public static void setListeners(CompoundButton view, final OnCheckedChangeListener listener,
final InverseBindingListener attrChange) {
if (attrChange == null) {
view.setOnCheckedChangeListener(listener);
} else {
view.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (listener != null) {
listener.onCheckedChanged(buttonView, isChecked);
}
attrChange.onChange();
}
});
}
}
}