val adapter = FirebaseRecyclerAdapter<Discount, Holder>(
Discount::class.java,
R.layout.fragment_main_day_item,
Holder::class.java,
FirebaseDatabase.getInstance().getReference()
) {
override fun populateViewHolder(holder: Holder, dis: Discount, pos: Int){
}
}
文档是here
如何使用Kotlin处理此问题
修改
val mAdapter = object : FirebaseRecyclerAdapter<Chat, ChatHolder>(
Chat::class.java,
R.layout.fragment_main_day_item,
ChatHolder::class.java,
ref) {
public override fun populateViewHolder(holder: ChatHolder, chat: Chat, position: Int) {
}
}
我将java转换为kotlin并且它有效。
答案 0 :(得分:4)
在upgrading到FirebaseUI 3.0后,Firebase实时数据库可以按照以下方式使用Kotlin
val options = FirebaseRecyclerOptions.Builder<Chat>()
.setQuery(chatQuery,Chat::class.java)
.setLifecycleOwner(this)
.build()
val adapter = object : FirebaseRecyclerAdapter<Chat, ChatHolder>(options) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChatHolder {
return ChatHolder(LayoutInflater.from(parent.context)
.inflate(R.layout.row_chat, parent, false))
}
protected override fun onBindViewHolder(holder: ChatHolder, position: Int, model: Chat) {
holder.bind(model)
}
override fun onDataChanged() {
// If there are no chat messages, show a view that invites the user to add a message.
mEmptyListMessage.setVisibility(if (itemCount == 0) View.VISIBLE else View.GONE)
}
}
答案 1 :(得分:1)
这显示了如何编写查询和简单的Holder类
var query = FirebaseDatabase.getInstance()
.reference
.child("").child("categories")
.limitToLast(50)
val options = FirebaseRecyclerOptions.Builder<Category>()
.setQuery(query, Category::class.java)
.setLifecycleOwner(this)
.build()
val adapter = object : FirebaseRecyclerAdapter<Category, CategoryHolder>(options) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryHolder {
return CategoryHolder(LayoutInflater.from(parent.context)
.inflate(R.layout.category_row, parent, false))
}
protected override fun onBindViewHolder(holder: CategoryHolder, position: Int, model: Category) {
holder.bind(model)
}
override fun onDataChanged() {
// Called each time there is a new data snapshot. You may want to use this method
// to hide a loading spinner or check for the "no documents" state and update your UI.
// ...
}
}
class CategoryHolder(val customView: View, var category: Category? = null) : RecyclerView.ViewHolder(customView) {
fun bind(category: Category) {
with(category) {
customView.textView_name?.text = category.name
customView.textView_description?.text = category.description
}
}
}