从firebase中恢复数据

时间:2018-01-08 16:08:39

标签: android firebase android-recyclerview kotlin android-image

我有数据,其中有图像发布和描述 当我检索数据时,它显示错误的吐司, 找不到什么错在这里是我的代码

class MainActivity : AppCompatActivity() {
    val ref = FirebaseDatabase.getInstance().getReference("post")
    var storageRef = FirebaseStorage.getInstance().reference
    private val TAG = ""
     override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
            val recyclerView = findViewById<RecyclerView>(R.id.DrawerId)
        recyclerView.layoutManager = LinearLayoutManager(this, LinearLayout.VERTICAL,false)
         val users = ArrayList<RecyClass>()
         val userlist = ArrayList<RecyClass>()

         ref.addChildEventListener(object :ChildEventListener{
                override fun onCancelled(p0: DatabaseError?) {
                    Log.e(TAG ,"Error is here")
                }
                override fun onChildMoved(p0: DataSnapshot?, p1: String?) {}
                override fun onChildChanged(p0: DataSnapshot?, p1: String?) {}
                override fun onChildAdded(p0: DataSnapshot?, p1: String?) {
                    if(p0!!.exists()){
                        for (h in p0.children) {
                        getpost(h.key)


                        }

                    }
                }
                override fun onChildRemoved(p0: DataSnapshot?) {}
            })



    }

    private fun getpost(key: String?) { 
        val postdata = FirebaseDatabase.getInstance().getReference("post").child(key)
        postdata.addListenerForSingleValueEvent(object:ValueEventListener{
            override fun onCancelled(p0: DatabaseError?) {
            }
            override fun onDataChange(p0: DataSnapshot?) {
                if(p0!!.exists()){
          val userlist = ArrayList<RecyClass>()
                 var descText = ""
                 var Image = ""
                 if (p0.child("desc").getValue()!=null){
 descText = p0.child(key).child("desc").getValue().toString()
               }
                if (p0.child("Image").getValue()!=null){
                     Image = p0.child(key).child("Image").getValue().toString()
                }
              val kkk = RecyClass(descText,Image)
                val adptr =RecyclerAdapter(userlist,applicationContext )
                  userlist.add(kkk)

                 adptr.notifyDataSetChanged()
             }else{
                    Toast.makeText(this@MainActivity,"error",Toast.LENGTH_LONG).show()
                }
            }

        }
        })
    }

我在firebase节点中已有多个数据 当我把吐司放在里面if(p0 !!。exists())它显示但是当吐司被移到里面时如果(p0.child(“desc”)。getValue()!= null)它很有效..

我在回收站视图中收到空白页

0 个答案:

没有答案