我已经阅读了所有类似的问题,但是找不到解决方案。没有任何错误。
能否请您查看这些代码并帮助查找为什么这些代码不显示Firebase数据的问题。查询有问题吗?还是RecyclerView?或适配器?
class OurResults : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_allresults)
actionBar.setBackgroundDrawable(resources.getDrawable(R.drawable.actionbar))
BeginFirebaseAuthListener()
Beginsicknesslistener()
}
var mValueEventListener:ValueEventListener=object :ValueEventListener{
override fun onCancelled(p0: DatabaseError) {
TODO("not implemented")
}
override fun onDataChange(p0: DataSnapshot) {
BringSickness()
}
}
private fun BringSickness() {
if(allsickness==null){
allsickness=ArrayList<usersickness>()
}
mySicknessReferans=FirebaseDatabase.getInstance().getReference()
var userid=FirebaseAuth.getInstance().currentUser?.uid
var myQuery=mySicknessReferans?.child("sickness/users/$user_id"+"/")!!
.addListenerForSingleValueEvent(object :ValueEventListener{
override fun onCancelled(p0: DatabaseError) {
TODO("not implemented")
}
override fun onDataChange(p0: DataSnapshot) {
for(Onesickness in p0!!.children){
var tempsickness=usersickness()
tempsickness.photo_result=Onesickness.getValue(usersickness::class.java)!!.photo_result
tempsickness.date=Onesickness.getValue(usersickness::class.java)!!.date
allsickness?.add(tempsickness)
myAdapter?.notifyDataSetChanged()
}
}
})
if(myAdapter==null){
initsicknesslist()
}
}
private fun initsicknesslist() {
myAdapter= SicknessRecyclerViewAdapter(this,allsickness!!)
rv_sickness.adapter=myAdapter
rv_sickness.layoutManager=LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
rv_sickness.scrollToPosition(myAdapter?.itemCount!!-1)
}
private fun Beginsicknesslistener() {
mySicknessReferans=FirebaseDatabase.getInstance().getReference()
.child("sickness/users/"+FirebaseAuth.getInstance().currentUser?.uid)
mySicknessReferans?.addValueEventListener(mValueEventListener)
}
private fun BeginFirebaseAuthListener() {
mAuthListener=object:FirebaseAuth.AuthStateListener{
override fun onAuthStateChanged(p0: FirebaseAuth) {
var myUser=p0.currentUser
if(myUser==null){
var intent=Intent(this@allresults,LoginActivity::class.java)
startActivity(intent)
finish()
}
}
}
}
override fun onStart() {
super.onStart()
FirebaseAuth.getInstance().addAuthStateListener (mAuthListener!!)
}
override fun onStop() {
super.onStop()
if(mAuthListener!=null){
FirebaseAuth.getInstance().removeAuthStateListener (mAuthListener!!)
}
}
override fun onResume() {
super.onResume()
}
}
数据库结构为: - 用户 -xGKiiLnf6TWhJeThkeKbZxtwHr1 -电子邮件: -照片: -用户身份: -gYJMhNRZ5XPMzAROsoNyQOmdVFd2 -电子邮件: -照片: -用户身份: - 疾病 -用户 -gYJMhNRZ5XPMzAROsoNyQOmdVFd2 -184b4d0f-9cd3-413b-b4ed-3b6485066 -日期: -photo_id:
答案 0 :(得分:0)
enter image description here 首先,您的ValueEventListener
是多余的。因此,无需从Beginsicknesslistener
致电BringSickness
到onCreate
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
.....
BeginFirebaseAuthListener()
//Beginsicknesslistener()
BringSickness()
}
然后解析BringSickness
内的疾病数据,如下所示:
private fun BringSickness() {
if(allsickness == null){
allsickness = ArrayList<usersickness>()
}
if(myAdapter == null){
initsicknesslist()
}
mySicknessReferans = FirebaseDatabase.getInstance().getReference()
var userid = FirebaseAuth.getInstance().currentUser?.uid
var myQuery = mySicknessReferans?.child("sickness/users/$user_id"+"/")!!
.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onCancelled(p0: DatabaseError) {
TODO("not implemented")
}
override fun onDataChange(p0: DataSnapshot) {
for(Onesickness in p0!!.children){
var tempsickness = Onesickness.getValue(usersickness::class.java)
if(tempsickness != null) {
allsickness?.add(tempsickness)
}
}
//Update adapter after collecting all the data
myAdapter?.notifyDataSetChanged()
}
})
}
建议:在编码过程中,请遵循standard naming convention。