我希望RecyclerView的每一行都显示集合中一个文档的所有详细信息。
我使用了完全相同的适配器代码,尽管将其序列化为其他类。而且效果很好。但是在这种情况下,它根本无法正常工作。 但是代码只是不会填充视图。
我的数据库就像:
reviews--Orange--vault--|
|-firstReview
|-secondReview
|-sjdeifhaih5aseoi
...
片段中的查询和适配器:
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(this).get(ReviewViewModel::class.java)
val reviewQuery = FirebaseFirestore.getInstance().collection("reviews").document("Orange").collection("vault")
val reviewBurnOptions = FirestoreRecyclerOptions.Builder<Review>()
.setQuery(reviewQuery, object : SnapshotParser<Review> {
override fun parseSnapshot(snapshot: DocumentSnapshot): Review {
return snapshot.toObject(Review::class.java)!!.also {
it.id = snapshot.id
}
}
}).setLifecycleOwner(this)
reviewRecycler.adapter=ReviewBurnAdapter(reviewBurnOptions.build())}
class ReviewBurnAdapter(options: FirestoreRecyclerOptions<Review>) :
FirestoreRecyclerAdapter<Review, ReviewBurnAdapter.ViewHolder>(options) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {//I never reach this point
val view = LayoutInflater.from(parent.context).inflate(R.layout.row_review, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int, item: Review) {
holder.apply {
holder.itemView.rowAuthor.text = item.author
}
}
inner class ViewHolder(override val containerView: View) :
RecyclerView.ViewHolder(containerView), LayoutContainer
}
要序列化为的类:
import com.google.firebase.firestore.Exclude
import com.google.firebase.firestore.PropertyName
import java.util.*
class Review(
@get:Exclude var id: String = "DEVIL",
@JvmField @PropertyName(AUTHOR) var author: String = "",
@JvmField @PropertyName(WRITEUP) var writeup: String = "",
//@JvmField @PropertyName(MOMENT) var moment:Date=Date(1997,12,1),
@JvmField @PropertyName(RATING) var rating: Int = 0
) {
companion object {
const val AUTHOR = "author"
const val WRITEUP = "writeup"
const val RATING = "rating"
//const val MOMENT="moment"
}
}
此外,没有错误,它永远不会到达将使用viewHolders生成和填充的代码。
答案 0 :(得分:1)
好的,修复非常简单,正如@Prashant Jha指出的那样,我尚未为RecyclerView -_-
指定布局管理器为清晰起见,我添加了app:layoutManager =“ androidx.recyclerview.widget.LinearLayoutManager”
到我的xml,一切正常。