找不到Android绑定适配器属性

编程入门 行业动态 更新时间:2024-10-23 02:02:39
本文介绍了找不到Android绑定适配器属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用绑定适配器在其中一个视图中包含可变文本.我相信我已经正确实现了它(并且在其他地方也可以使用),但是对于mutableText来说,它得到了错误AAPT: error: attribute mutableText not found

I am using a binding adapter to have mutable text in one of my views. I believe I have it implemented correctly (and it's working in other places), but for the case of mutableText, it's getting the error AAPT: error: attribute mutableText not found

我在这里浏览了其他一些答案,但是没有一个能够解决问题.

I've looked through some other answers on here, but none of them have been able to solve the issue.

这是我的布局文件:

<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="schemas.android/apk/res/android" xmlns:tools="schemas.android/tools" xmlns:app="schemas.android/apk/res-auto"> <data> <variable name="viewModel" type="com.example.nhlstats.ui.game.GameViewModel" /> </data> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/awayTeam" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="12dp"> <ImageView android:id="@+id/awayTeamLogo" android:layout_height="48dp" android:layout_width="0dp" android:layout_weight="1" tools:src="@drawable/ic_launcher_background"/> <TextView android:id="@+id/awayTeamName" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:layout_gravity="center_vertical" app:mutableText="@{viewModel.getAwayTeamName()}" tools:text="CHI Blackhawks"/> <TextView android:id="@+id/awayScore" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="center_vertical" app:mutableText="@{viewModel.getAwayTeamScore().toString()}" tools:text="0"/> <TextView android:id="@+id/gameTime" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="center_vertical" app:mutableText="@{viewModel.getTimeRemaining()" tools:text="14:26 3rd"/> </LinearLayout> <LinearLayout android:id="@+id/homeTeam" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="12dp"> <ImageView android:id="@+id/homeTeamLogo" android:layout_height="48dp" android:layout_width="0dp" android:layout_weight="1" tools:src="@drawable/ic_launcher_background"/> <TextView android:id="@+id/homeTeamName" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:layout_gravity="center_vertical" app:mutableText="@{viewModel.getHomeTeamName()}" tools:text="CAR Hurricanes"/> <TextView android:id="@+id/homeScore" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="2" app:mutableText="@{viewModel.getHomeTeamScore().toString()}" tools:text="4"/> </LinearLayout> </LinearLayout> </layout>

和我的BindingAdpater函数:

and my BindingAdpater function:

@BindingAdapter("mutableText") fun setMutableText(view: TextView, text: MutableLiveData<String>?) { val parentActivity:AppCompatActivity? = view.getParentActivity() if (parentActivity != null && text != null) { text.observe(parentActivity, Observer { value -> view.text = value?:"" }) } }

GameViewModel:

GameViewModel:

class GameViewModel:BaseViewModel() { private val awayTeamName = MutableLiveData<String>() private val homeTeamName = MutableLiveData<String>() private val awayTeamScore = MutableLiveData<Int>() private val homeTeamScore = MutableLiveData<Int>() private val timeRemaining = MutableLiveData<String>() fun bind(response: Game) { awayTeamName.value = response.gameData.teams.get(0).name homeTeamName.value = response.gameData.teams.get(1).name awayTeamScore.value = response.liveData.linescore.teams.get(1).goals homeTeamScore.value = response.liveData.linescore.teams.get(0).goals timeRemaining.value = response.liveData.linescore.currentPeriodOrdinal + " " + response.liveData.linescore.currentPeriodTimeRemaining } fun getAwayTeamName(): MutableLiveData<String> { return awayTeamName } fun getHomeTeamName(): MutableLiveData<String> { return homeTeamName } fun getAwayTeamScore(): MutableLiveData<Int> { return awayTeamScore } fun getHomeTeamScore(): MutableLiveData<Int> { return homeTeamScore } fun getTimeRemaining(): MutableLiveData<String> { return timeRemaining } }

MainActivity:

MainActivity:

class MainActivity : AppCompatActivity() { private lateinit var binding: ActivityMainBinding private lateinit var viewModel: GameListViewModel private var errorSnackbar: Snackbar? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = DataBindingUtil.setContentView(this, R.layout.activity_main) binding.gameList.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false) viewModel = ViewModelProviders.of(this).get(GameListViewModel::class.java) viewModel.errorMessage.observe(this, Observer { errorMessage -> if (errorMessage != null) showError(errorMessage) else hideError() }) binding.viewModel = viewModel } private fun showError(@StringRes errorMessage:Int) { errorSnackbar = Snackbar.make(binding.root, errorMessage, Snackbar.LENGTH_INDEFINITE) errorSnackbar?.setAction(R.string.retry, viewModel.errorClickListener) errorSnackbar?.show() } private fun hideError() { errorSnackbar?.dismiss() } }

谢谢.

推荐答案

您的绑定适配器收到一个可为空的值,而viewModel返回一个非null的值.将text参数更改为非null,就像viewModel中的函数返回它一样.

Your binding adapter receives a nullable value, while the viewModel returns a non-null value. Change the text parameter to be non-null, just like the function in the viewModel returns it.

您还可以进行较短的封装,例如:

You can also do shorter encapsulation, for example:

private val _data = MutableLiveData<String>() val data: LiveData<String> get() = _data

这样,您不必像Java那样编写冗长而乏味的getter.

This way you don't have to write long and boring getters like in Java.

更多推荐

找不到Android绑定适配器属性

本文发布于:2023-08-07 13:51:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319401.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:找不到   适配器   绑定   属性   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!