【发布时间】:2020-10-16 00:23:57
【问题描述】:
我正在开发一个足球静态应用程序,我使用了绑定适配器,但在我的 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>()
我尝试解决的问题:
- 缓存重启无效
- 关注此Android Binding Adapter attribute not found 链接。尝试了建议的答案,但它没有解决我的问题
为了避免这个问题,我必须做什么?任何帮助和建议将不胜感激。
【问题讨论】:
-
您的某些 mutableText 语句中似乎缺少一些大括号。
app:mutableText="@{viewModel.clubValue"。这是格式错误还是实际上在您的代码中?如果是后者,您可能应该更正它。 -
@Cheesebaron 我已经解决了问题,但现在我遇到了另一个问题,也许你可以查看那个帖子
-
你能不能把你的帖子的答案放在这里。
标签: android kotlin mvvm data-binding two-way-binding