android - Android kotlin 两种方式数据绑定问题
问题描述
我正在尝试在回收站视图中使用数据绑定,但我遇到了问题。在数据变量上,我无法选择我的真实类型(相反,我可以选择类型作为默认包。然后我无法访问我的适配器类上的变量。我在线上的适配器出现错误:binding.curMovie = movie .我得到的错误是:无法访问类movieItem检查您的模块类路径是否缺少或冲突的依赖项绑定
电影项目.kt 文件
import com.google.gson.annotations.SerializedName
data class MovieItem(
@SerializedName("popularity") val popularity: Double,
@SerializedName("id") val id: Int,
@SerializedName("video") val video: Boolean,
@SerializedName("vote_count") val vote_count: Int,
@SerializedName("vote_average") val vote_average: Double,
@SerializedName("title") val title: String?,
@SerializedName("release_date") val release_date: String?,
@SerializedName("original_language") val original_language: String?,
@SerializedName("original_title") val original_title: String?,
@SerializedName("genre_ids") val genre_ids: List<Int>,
@SerializedName("backdrop_path") val backdrop_path: String?,
@SerializedName("adult") val adult: Boolean,
@SerializedName("overview") val overview: String?,
@SerializedName("poster_path") val poster_path: String?
)
list_item.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"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="curMovie"
type="MovieItem" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:text="@{curMovie.title}"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
app:layout_constraintBottom_toBottomOf="parent"
tools:text="my movie" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
电影适配器.kt 文件
class MovieAdapter() : RecyclerView.Adapter<MovieAdapter.MovieViewHolder>() {
var moviesList: List<MovieItem> = ArrayList()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MovieViewHolder {
val inflater = LayoutInflater.from(parent.context)
val binding = ListItemBinding.inflate(inflater)
return MovieViewHolder(binding)
}
override fun getItemCount(): Int {
return moviesList.size
}
override fun onBindViewHolder(holder: MovieViewHolder, position: Int) {
val movie = moviesList.get(position)
holder.setMovieData(movie)
}
class MovieViewHolder(val binding: ListItemBinding) : RecyclerView.ViewHolder(binding.root) {
fun setMovieData(movie: MovieItem) {
binding.curMovie = movie
}
}
fun setMovieList(moviesList : List<MovieItem>){
this.moviesList = moviesList
notifyDataSetChanged()
}
}
解决方案
最有可能的是,包名称以大写字母https://stackoverflow.com/a/54752522/11738109或在 list_item.xml 中使用完整的类路径而不是简短的 MovieItem
推荐阅读
- python - AWS lambda python Socket 异常:不允许操作 (1)
- parameters - 我的 Dialogflow 代理的第二个后续意图无法捕获其父参数的值
- html - 在 html 输入中保留初始空格
- python - 矩阵操作:在 numpy 中减去 2D 矩阵和 3D 矩阵
- soap - 创建在生成的请求中显示的评论?WSDL SOAP xml
- python - 在 python 中使用路由器的最佳实践是什么?
- jsf-1.2 - 将 JSF1.2 应用程序从 Jboss 4.2 迁移到 Wildfly 13
- java - IntelliJ IDEA 中的逗号优先 Java 格式
- yii - 获取模块配置参数
- tensorflow - 如何在 Jetson TX1 上更新 Protocol Buffer 运行时库?