Android 如何从firebase立即获取recyclerview项目?
请帮助获取这一点,我只是想当用户添加firebase上的内容时,这个特定的节点将显示在recyclerview中,用户点击可删除的回收项目上的叉号Android 如何从firebase立即获取recyclerview项目?,android,firebase,kotlin,firebase-realtime-database,Android,Firebase,Kotlin,Firebase Realtime Database,请帮助获取这一点,我只是想当用户添加firebase上的内容时,这个特定的节点将显示在recyclerview中,用户点击可删除的回收项目上的叉号 private fun insetcontent(bannerText: String, titelText: String, designationtext: String, descriptionText1: String) { var databaseReference = FirebaseDatabase.getInstance()
private fun insetcontent(bannerText: String, titelText: String, designationtext: String, descriptionText1: String) {
var databaseReference = FirebaseDatabase.getInstance().getReference("Content")
var key = databaseReference.push().key!!
var hasmap:MutableMap<String,String> = hashMapOf(
"contentId" to key,
"movieImage" to "default",
"bannerName" to bannerText,
"titleName" to titelText,
"designation_text" to designationtext,
"description" to descriptionText1,
)
databaseReference.child(key).setValue(hasmap).addOnCompleteListener {
if (it.isSuccessful) {
view?.content_progressbasr!!.visibility = View.GONE
Toast.makeText(requireContext(), "Content added successfully", Toast.LENGTH_SHORT)
.show()
databaseReference.addChildEventListener(object : ChildEventListener{
override fun onChildAdded(snapshot: DataSnapshot, previousChildName: String?) {
for(snapshotone in snapshot.)
{
var listadata = snapshotone.getValue(ContentClass::class.java)
list.add(listadata!!)
}
view!!.content_recyclerview.apply {
var contentadapter = ContentAdapter(list,requireView())
this.adapter = contentadapter
}
}
override fun onChildChanged(
snapshot: DataSnapshot,
previousChildName: String?
) {
}
override fun onChildRemoved(snapshot: DataSnapshot) {
}
override fun onChildMoved(snapshot: DataSnapshot, previousChildName: String?) {
}
override fun onCancelled(error: DatabaseError) {
}
})
} else {
view?.content_progressbasr!!.visibility = View.GONE
Toast.makeText(
requireContext(),
"error : ${it.exception.toString()}",
Toast.LENGTH_SHORT
).show()
}
}
}
private fun insertcontent(bannerText:String,titelText:String,designationtext:String,descriptionText1:String){
var-databaseReference=FirebaseDatabase.getInstance().getReference(“内容”)
var key=databaseReference.push().key!!
var hasmap:MutableMap=hashMapOf(
“contentId”到键,
“电影图像”改为“默认值”,
“bannerName”到bannerText,
“titleName”到titelText,
“指定文字”至指定文字,
“描述”用于描述Context1,
)
databaseReference.child(key).setValue(hasmap.addOnCompleteListener{
如果(it.ISSUCCESS){
查看?.content\u progressbasr!!.visibility=view.GONE
Toast.makeText(requireContext(),“内容添加成功”,Toast.LENGTH\u SHORT)
.show()
databaseReference.addChildEventListener(对象:ChildEventListener{
覆盖有趣的onChildAdded(快照:DataSnapshot,previousChildName:String?){
用于(快照中的快照一)
{
var listadata=snapshotone.getValue(ContentClass::class.java)
list.add(listadata!!)
}
查看!!.content\u recyclerview.apply{
var contentadapter=contentadapter(列表,requireView())
this.adapter=contentadapter
}
}
改变孩子的生活(
快照:DataSnapshot,
previousChildName:字符串?
) {
}
重写ChildRemoved(快照:DataSnapshot){
}
重写趣味onChildMoved(快照:DataSnapshot,previousChildName:String?){
}
覆盖已取消(错误:DatabaseError){
}
})
}否则{
查看?.content\u progressbasr!!.visibility=view.GONE
Toast.makeText(
requireContext(),
“错误:${it.exception.toString()}”,
吐司长度
).show()
}
}
}
上面是我的firebase逻辑,我再次希望最后添加的子项将显示在recyclerview中这段代码中到底有什么不符合您的预期?它不会显示用户刚刚添加的recyclerview中的子项,因此,如果不合适,他可以查看提交的响应,然后删除此节点。这是添加addchildvalueeventlistener后我期望从该代码中获得的行为类型。请立即添加该子节点,请帮助我,先生