如何更改firebase数据的值,它的databaseRef与valueEventListener存在于不同的类中

时间:2019-04-22 15:19:09

标签: android firebase kotlin

我有ratingBar片段。如果在此处使用数据库引用,数据将发生变化,但是当我返回并再次尝试输入应用程序时会崩溃,因为此时仅添加了有关评分栏的数据

我尝试了界面,但是当我这样做时,片段有:

  

二进制XML文件第7行:膨胀类片段时出错

//Fragment
override fun onRatingChanged(ratingBar: RatingBar?, rating: Float, fromUser: Boolean) {

        // val mDatabase: DatabaseReference = FirebaseDatabase.getInstance().getReference("uploads")
        //val itemReference = mDatabase.child(objectId)
        //itemReference.child("stars").setValue(rating.toDouble())
        setStars(rating.toDouble())
    }
//Activity
        mDatabaseRef = FirebaseDatabase.getInstance().getReference("uploads")

        mDatabaseRef.addValueEventListener(object : ValueEventListener {
            override fun onDataChange(dataSnapshot: DataSnapshot) {
                mUploads.clear()
                for (postSnapshot in dataSnapshot.children) {
                    val upload: Upload = postSnapshot.getValue(Upload::class.java)!!
                    (mUploads).add(upload)
                }

                mAdapter.notifyDataSetChanged()

                mProgressCircle.visibility = View.INVISIBLE
            }

            override fun onCancelled(databaseError: DatabaseError) {
                Toast.makeText(this@ImagesActivity, databaseError.message, Toast.LENGTH_SHORT).show()
                mProgressCircle.visibility = View.INVISIBLE
            }
        })

    }
//My interface try
    override fun modifyItemState(itemObjectId: String, stars: Double) {
        // val mDatabase: DatabaseReference = FirebaseDatabase.getInstance().getReference("uploads")
        val itemReference = mDatabaseRef.child(itemObjectId)
        itemReference.child("stars").setValue(stars)
    }

it was change from 0 when i want back to this fragmentactivity, stars is created

0 个答案:

没有答案