首页 > 解决方案 > : 属性 mutableText (aka yodgorbekkomilov.edgar.footballapp:mutableText) 未找到。?

问题描述

我正在开发一个足球静态应用程序,我使用了绑定适配器,但在我的football_item.xml文件中出现以下错误

C:\Users\Edgar\Desktop\FootballApp\app\src\main\res\layout\football_item.xml:17: AAPT: error: attribute mutableText (aka yodgorbekkomilov.edgar.footballapp:mutableText) not found.

¨

下面是我收到错误的 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>
    <variable
        name="viewModel"
        type="yodgorbekkomilov.edgar.footballapp.ui.FootballViewModel" />
</data>


<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:mutableText="@{viewModel.clubName"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:mutableText="@{viewModel.countryName"/>





    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:mutableText="@{viewModel.clubValue"/>

    <ImageView
        app:imageUrl="@{viewModel.image}"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:id="@+id/clubImage"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:mutableText="@{viewModel.europeanTitle}"/>


</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

以下是我bindingAdapter使用过的课程bindingadapter

@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?:""})
    }

下面是我的FootballListViewModel.kt

class FootballViewModel: BaseViewModel() {
    private val clubName = MutableLiveData<String>()

    private val countryName = MutableLiveData<String>()
    private val clubValue = MutableLiveData<Int>()
    private val clubImage = MutableLiveData<String>()
    private val europeanTitle = MutableLiveData<Int>()

    fun bind(football: FootballResponse){
        clubName.value= football[0].name
        countryName.value = football[1].country
        clubValue.value = football[2].value
        clubImage.value = football[3].image
        europeanTitle.value = football[4].europeanTitles

    }

    fun getClubName():MutableLiveData<String>{
        return clubName
    }

    fun getCountryName():MutableLiveData<String>{
        return countryName
    }

    fun getClubValue():MutableLiveData<Int>{
        return clubValue
    }

    fun getImage():MutableLiveData<String> {
 return clubImage

    }


    fun getEuropeanTitle():MutableLiveData<Int> {
        return europeanTitle

    }
}

下边是FootballResponseItem.kt

data class FootballResponseItem(
    @SerializedName("country")
    val country: String,
    @SerializedName("european_titles")
    val europeanTitles: Int,
    @SerializedName("image")
    val image: String,
    @SerializedName("name")
    val name: String,
    @SerializedName("value")
    val value: Int
)

下边是FootballResponse.kt

class FootballResponse : ArrayList<FootballResponseItem>()

我试图解决的问题:

  1. 使缓存重新启动无效
  2. 遵循此Android Binding Adapter 属性未找到链接。尝试了建议的答案,但它没有解决我的问题

为了避免这个问题,我必须做什么?任何帮助和建议将不胜感激。

标签: androidkotlinmvvmdata-bindingtwo-way-binding

解决方案


我已经更改了我的 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>
    <variable
        name="viewModel"
        type="yodgorbekkomilov.edgar.footballapp.ui.FootballViewModel" />
</data>


<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:mutableText="@{viewModel.clubName}"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:mutableText="@{viewModel.countryName}"/>





    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:mutableText="@{viewModel.clubValue}"/>

    <ImageView
        app:imageUrl="@{viewModel.image}"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:mutableText="@{viewModel.europeanTitle}"/>


</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

推荐阅读