【问题标题】:two way binding with null check带有空检查的两种方式绑定
【发布时间】:2018-05-04 13:42:01
【问题描述】:

在数据绑定adatper 中,我想检查我的模型中的int 值是否不为零。因为从不显示提示,如果默认值为 0,则 0 显示为文本。如果值为零,我想显示提示。

在不检查 0 int 值的情况下运行良好

   <android.support.design.widget.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/port"
                android:inputType="number"
                android:text="@={`` + item.port}"
                />

我试过这个不起作用

   <android.support.design.widget.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/port"
                android:inputType="number"
                android:text='@={item.port != 0 ? `` + item.port : ""}'
                />

item.portint

有什么建议可以仅使用数据绑定

【问题讨论】:

  • item.port 是什么类型?
  • @H.Taras 它是 int
  • 它是类似于使用提示的标志还是它的目的?
  • 这是双向绑定,如果我在editext中更改值,则应该更改模型int值

标签: android data-binding android-databinding


【解决方案1】:

我认为您需要BindingAdapter/InverseBindingAdapter 或转换方法。最简单的可能是转换方法:

<layout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">
    <data>
        <import type="com.example.mount.teststuff.Conversion"/>
        <variable name="port" type="int"/>
    </data>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <EditText
            android:id="@+id/input"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@={Conversion.intToString(port, port)}"
            android:textSize="40sp"/>
        <TextView
            android:id="@+id/output"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{`` + port}"
            android:textSize="40sp"/>
    </LinearLayout>
</layout>

在你的 Conversion 类中你会有这样的东西:

public class Conversion {
    @InverseMethod("stringToInt")
    public static String intToString(int oldValue, int value) {
        if (value == 0) {
            return null;
        }
        return String.valueOf(value);
    }

    public static int stringToInt(int oldValue, String value) {
        if (value == null || value.isEmpty()) {
            return 0;
        }
        try {
            return Integer.parseInt(value);
        } catch (NumberFormatException e) {
            return oldValue;
        }
    }
}

我刚刚更新了答案以包含我测试过的布局和代码。您可以在this blog post 上查找更多详细信息。

【讨论】:

  • 在文本参数中使用转换类后,Gradle 构建失败。
  • 另外一件坏事是构建在绑定类中显示错误,它不能说明是什么问题。
  • 您是否导入了Conversion 类?我不确定它为什么失败。
  • 是的,我导入了:/,你能用虚拟数据检查一下吗
  • 我刚刚尝试了这个确切的代码并且没有遇到任何问题。我会用完整的代码更新答案。
【解决方案2】:

&lt;import type="Integer"/&gt;

<android.support.design.widget.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/port"
                android:inputType="number"
                android:text='@={item.port}'
                />

不要将端口变量保留为 int,因为双向绑定需要 String 类型的 Setter 用于端口值。而是在 XML 本身中将 String 值转换为 Integer。

更新:请从您的视图模型中更改端口值,而不是检查 null 以获取提示。如果端口为0,则可以将其替换为空字符串“”,然后调用 notifyPropertyChange(BR.port);

【讨论】:

  • 这可能是一个解决方案。您在提示中检查了 int 0。酷
  • java.lang.NumberFormatException: Invalid int: "null"
  • 我认为当字符串端口中没有值时提示出现此错误。
  • 如你所说,我已将 item.port 从整数更改为字符串
猜你喜欢
  • 2016-02-27
  • 1970-01-01
  • 2017-09-05
  • 2014-02-13
  • 1970-01-01
  • 2012-01-14
  • 2018-05-24
  • 2017-01-03
  • 2018-12-09
相关资源
最近更新 更多