gpt4 book ai didi

java - 使用数据绑定(bind)时,不会调用自定义 setter

转载 作者:太空宇宙 更新时间:2023-11-04 13:22:56 25 4
gpt4 key购买 nike

我刚刚在使用 android 数据绑定(bind)库时遇到了问题。

这是 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">

<data>
<import type="com.test.app.ObservableFieldWrapper"/>
<variable
name="org"
type="ObservableFieldWrapper"/>
</data>
<LinearLayout
android:id="@+id/headerListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.test.app.NSpinner
android:id="@+id/orgSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:org="@{org.getSilent ? org.content : &quot;silent&quot;}"/>
</LinearLayout>

这是我的 NSpinner:

 public class ObservableFieldWrapper{

private final ObservableBoolean silent;

private final ObservableField<String> content;

@BindingAdapter("org")
public static void setOrg(Spinner view, String org) {
assert org != null;
if (org.equals("silent")) {
Log.i("ObsWrapper", "SET ORG called via binding adapter but got denied, because of SILENCE");
} else {
Log.i("ObsWrapper", "SET ORG called via binding adapter NORMALLY");
view.setSelection(Cache.GetOrgIndexForSpinner(), true);
}
}

public ObservableFieldWrapper(String startValue) {
content = new ObservableField<>(startValue);
silent = new ObservableBoolean();
silent.set(false);
}

public void setContent(String newValue) {
silent.set(false);
content.set(newValue);
content.notifyChange();
}

public void setContentSilent(String newValue) {
silent.set(true);
content.set(newValue);
}

//Bunch of getters
}

此调用应调用 ObservableFieldWrapper 类提供的静态 getter(假设所有绑定(bind)均已设置):

ObservableFieldWrapper someField = new ObservableFieldWrapper("someString");
someField.setContent("some other string");

嗯,问题是...它没有调用任何东西。但是如果我改变我的 xml 部分

app:org="@{org.getSilent ? org.content : &quot;silent&quot;}"

常见

app:org="@{org.content}"

它开始工作了!我确实需要这个带有 boolean 值的额外功能,但我在寻找问题时真的迷失了方向。

<小时/>

找到了一个解决方法,其中没有在 xml 表达式中使用任何逻辑,我只是​​将 2 个参数传递给我的函数并在那里完成了所有工作。

@Bindable ("{org, silent}")

然而,这个问题仍然没有答案。

最佳答案

正如 George Mount 提到的 - 删除可观察字段上的任何 getter 非常重要,否则它将无法工作,我花了很多时间来解决这个问题,然后提到我有一个 getter,删除它后 - 一切都开始工作。

关于java - 使用数据绑定(bind)时,不会调用自定义 setter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32906361/

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