首页 > 解决方案 > Textview中的Android条件字符串,未评估数据绑定

问题描述

我现在已经搜索了网络,但没有找到解决我这个问题的方法。

我的问题是我有一个TextView并且我想在其中使用该android:text属性显示文本。@string应根据FLAVOR正在运行的内容以有条件的方式从中获取此文本。

我已经将我的 xml 布局文件包装在<layout></layout>标签中,并且在这些标签中我导入了一些类:

 <data>
    <import type="esy.es.matmatt.pidroCounter.BuildConfig"/>
    <import type="android.view.View"/>
</data>

要根据 FLAVOR 有条件地在 Textview 中显示不同的文本,我有以下 TextView:

<TextView
        android:text="@{String.valueOf(BuildConfig.FLAVOR.equals(`free`) ? @string/about_text : @string/about_textPro)}"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textViewFree"
        android:layout_below="@+id/aboutTitle"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textAlignment="center"
        android:autoLink="email"
        android:textColorLink="@color/textn"/>

我不知道为什么这只是输出一个空白的TextView...它一个都不显示...

我在这篇文章中读到我应该在@之后有一个=符号,但是当我添加它时,我只是得到了一个编译错误:

M:\Git Repositories\PidroCounter\app\build\generated\ap_generated_sources\freeDebug\out\esy\es\matmatt\pidroCounter\DataBinderMapperImpl.java:10: error: cannot find symbol import esy.es.matmatt.pidroCounter.databinding.FragmentAboutBindingImpl;
                                          ^

我不能简单地找到错误。我使用的导入和条件语法似乎在广告视图的可见性上工作得很好,但在 TextViews 上根本不行?我不知道我做错了什么。有任何想法吗?谢谢!

编辑: 实际上我现在意识到这些条件在广告视图中也不起作用(XML)。我认为这是因为我也有条件地以MobileAds编程方式加载......

标签: javaandroidandroid-layoutandroid-databinding

解决方案


我认为您的问题存在于您编写的绑定表达式中,因为使用了String.valueOf. 它应该是

android:text="@{BuildConfig.FLAVOR.equals(`free`) ? @string/about_text : @string/about_textPro}"

如果这不起作用,请打开数据绑定生成的BindingImpl.java(例如:)类。YourLayoutNameBindingImpl.java搜索您的视图 ID ( textViewFree) 并在此处发布生成的代码。


推荐阅读