RecyclerView 正在循环从 firebase 检索到的相同数据

RecyclerView is looping the same data retrieved from firebase

我试图向 firebase 添加一个新数据,然后在 recyclerview 中显示它,但是在我添加数据之后,recyclerview 只是循环整个数据,直到数据完成上传,从而创建一个这样的视图: the looped recyclerview

正如您在图片中看到的那样 link,我尝试添加“食物 6”数据,但作为添加过程的结果,recyclerview 不断更新其中的项目,直到添加过程完成

这是我的适配器代码

class FoodAdapter (private var dataList : ArrayList<Food>): RecyclerView.Adapter<FoodAdapter.MyViewholder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewholder {
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.food_recyclerview,parent,false)
        return MyViewholder(itemView)
    }

    override fun onBindViewHolder(holder: MyViewholder, position: Int) {
        val currentItem = dataList[position]
        Log.w("adapater",currentItem.image_pic.toString())
        Picasso.get().load(currentItem.image_pic).into(holder.foodPic)
        holder.food_name.text = currentItem.name
        holder.food_price.text = currentItem.price.toString()
        if (currentItem.avail == true){
            holder.food_avail.text = "Tersedia"
            holder.food_avail.setTextColor(Color.GREEN)
        } else {
            if (currentItem.avail == false){
                holder.food_avail.text = "Habis"
                holder.food_avail.setTextColor(Color.RED)
            } else {
                holder.food_avail.text = "Error"
            }
        }

    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    inner class MyViewholder (itemView : View): RecyclerView.ViewHolder(itemView){

        val foodPic : ImageView = itemView.findViewById(R.id.iv_gambar_makanan)
        val food_name : TextView = itemView.findViewById(R.id.tv_nama_makanan)
        val food_avail : TextView = itemView.findViewById(R.id.tv_status_makanan)
        val food_price : TextView = itemView.findViewById(R.id.tv_harga_makanan)
    }

}

这是我对 firebase 代码的更新数据

    private fun addDatatoFirebase() {
        val dataRef = ref.child(preferences.getValue("username").toString()).child("FoodList/"+ UUID.randomUUID().toString())
        var PicUrl = ""
        val addImage = StorageRef.child(preferences.getValue("username").toString())
            .child("food_pics/" + UUID.randomUUID())
        Log.i("Cycle", "Add Image to Firebase")
        addImage.putFile(FilePath).addOnSuccessListener {
            addImage.downloadUrl.addOnSuccessListener {
                PicUrl = it.toString()
                dataRef.child("image_pic").setValue(PicUrl)
            }
        }
        Log.i("URL",addImage.toString())
        dataRef.addValueEventListener(object : ValueEventListener{
            override fun onDataChange(snapshot: DataSnapshot) {
                id = snapshot.ref
                Log.w("PicUrl data",PicUrl)
                dataRef.child("name").setValue(food_name)
                dataRef.child("avail").setValue(availability)
                dataRef.child("price").setValue(food_price.toInt())
            }

            override fun onCancelled(error: DatabaseError) {
                Toast.makeText(applicationContext,"Error",Toast.LENGTH_SHORT)
            }

        })
    }

这是获取数据的代码

   private fun getFoodData() {
        val foodData = ref.child(preferences.getValue("username").toString()).child("FoodList")
        foodData.addValueEventListener(object : ValueEventListener{
            override fun onDataChange(snapshot: DataSnapshot) {
                if (snapshot != null) {
                    for (userSnapshot in snapshot.children) {
                        var data = userSnapshot.getValue(Food::class.java)
                        foodDataArrayList.add(data!!)
                    }
                }
                foodList.adapter = FoodAdapter(foodDataArrayList)
            }

            override fun onCancelled(error: DatabaseError) {
                Toast.makeText(applicationContext,"Error",Toast.LENGTH_SHORT)
            }

        })

        foodList.adapter = FoodAdapter(foodDataArrayList)
    }

谁能告诉我如何解决这个问题?

你漏掉了一件事。每次数据更改时,您都会一次又一次地获取所有条目,而不会删除之前的 un-updated 数据。因此,简单的解决方案是在获取数据之前清除列表。试试这个代码:

private fun getFoodData() {
        val foodData = ref.child(preferences.getValue("username").toString()).child("FoodList")
        foodData.addValueEventListener(object : ValueEventListener{
            override fun onDataChange(snapshot: DataSnapshot) {
                if (snapshot != null) {
                    foodDataArrayList.clear() // added this line
                    for (userSnapshot in snapshot.children) {
                        var data = userSnapshot.getValue(Food::class.java)
                        foodDataArrayList.add(data!!)
                    }
                }
                foodList.adapter = FoodAdapter(foodDataArrayList)
            }

            override fun onCancelled(error: DatabaseError) {
                Toast.makeText(applicationContext,"Error",Toast.LENGTH_SHORT)
            }

        })
    }