gpt4 book ai didi

java - 将 boolean 值绑定(bind)到复选框 MVVM

转载 作者:行者123 更新时间:2023-11-30 00:00:31 25 4
gpt4 key购买 nike

我试图通过 View 模型将 boolean 值绑定(bind)到 View ,但 Android Studio 抛出错误,我找不到问题所在。 viewModel.infilltype 是一个 boolean 值,android:checked 也应该是一个 boolean 值。

“错误:'@{viewModel.infillType}' 与属性 android:checked (attr) boolean 值不兼容。Message{kind=ERROR, text=error: '@{viewModel.infillType}' 与属性 android:checked (attr) boolean 不兼容。, sources=[E:\SportveldOnderhoud\app\src\main\res\layout\list_item_check .xml:14], original message=, tool name=Optional.of(AAPT)}"

我有以下代码(我会粘贴相关的 fragment )

Check.java(模型)

public class Check {

private UUID mId;
private String mTitle;
private Date mDate;
....
private boolean mInfilltype;
....

public Check() {
this(UUID.randomUUID());
}

public Check(UUID id) {
mId = id;
mDate = new Date();
}
public UUID getId() {
return mId;
}

....

public boolean isInfilltype() {
return mInfilltype;
}

public void setInfilltype(boolean infilltype) {
mInfilltype = infilltype;
}
}

View 模型:

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

查看:

<?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>

最佳答案

原来还得加上:

android { 
...
dataBinding {
enabled = true
}

在 build.gradle 中

然后它仍然没有工作,但是在将 maven 添加到 build.gradle 文件中的存储库之后它确实工作了

关于java - 将 boolean 值绑定(bind)到复选框 MVVM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50018516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com