使用 Google Firebase 从 Recycler View 列表中删除数据

问题描述 投票:0回答:1

我正在构建一个连接到 Google Firestore 的应用程序,允许用户从 Recycler View 列表中添加和删除电视节目数据。我的问题是尝试通过一项活动询问用户节目的名称、导演和收视率来从列表中删除数据。在我输入活动信息并按下按钮后,它不会从列表中删除该信息并且仍然存在。 Activity that allows you to remove data from Recycler View list

至于代码。

override fun onResume() {
    super.onResume()

    db = FirebaseFirestore.getInstance()
    data = ArrayList<TV>()

    tvAdapter = TVAdapter(data)

    var removeTV = findViewById(R.id.removeButton) as Button    //Button that removes data after       user enters rest of data.
    var name = findViewById(R.id.editTextShowName2) as EditText //Shows name
    var dir = findViewById(R.id.editTextDirector2) as EditText  //Shows director
    var view = findViewById(R.id.editTextNumber2) as EditText   //Shows viewership

    removeTV.setOnClickListener{
        val showN = name.text.toString()
        val d = dir.text.toString()
        val v = view.text.toString()
        
        // Section for making notifications when user removes data from RV list
        val intent = Intent(this, AddShowActivity::class.java).apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        }
        val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)

        var builder = NotificationCompat.Builder(this, "CHANNEL_ID")
            .setSmallIcon(R.drawable.notification_message)
            .setContentTitle("Notification")
            .setContentText("Show Removed!")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true)

        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.notify(1, builder.build())
        // End of notification Section

        val data = hashMapOf(
            "name" to "${showN}",
            "director" to "${d}",
            "viewership" to "${v}"
        )
        
        // Google Firebase that suppose to remove data from list and any data from collection that is there.
        db.collection("ProjectTVShows").document("-") //Want it to remove data that the user                entered from the Removing Activity.
            .delete()
            .addOnSuccessListener { documentReference ->
                Log.d("MY DEBUG", "DocumentSnapshot removed with ID")

                tvAdapter.notifyDataSetChanged()

                var data1 = name.text.toString()
                var data2 = dir.text.toString()
                var data3 = view.text.toString()
            }
            .addOnFailureListener { e ->
                Log.w("MY DEBUG", "Error adding document", e)
            }

        finish() //After button is pressed it goes back to the main activity.
    }
}

private fun createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val name = "Channel Name"
        val descriptionText = "Channel Description"
        val importance = NotificationManager.IMPORTANCE_DEFAULT
        val channel = NotificationChannel("CHANNEL_ID", name, importance).apply {
            description = descriptionText
        }
        // Register the channel with the system
        val notificationManager: NotificationManager =
            getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.createNotificationChannel(channel)
    }
}

}

我尝试在 Google 上查找文档并在线观看视频,看看是否能找到与我的问题有任何相似之处。还尝试多次更改代码。

android firebase kotlin android-recyclerview
1个回答
0
投票

当您致电

delete()
获取以下文档参考时:

db.collection("ProjectTVShows").document("-").delete()
//                                       👆

这意味着您要删除 ID 为

-
的文档,但由于不存在具有该 ID 的文档,因此无法执行此操作。如果您想删除具有
SmRYuGol1R88vqLfD2VR
ID 的文档,那么您应该将该 ID 传递给
document()
方法:

db.collection("ProjectTVShows").document("SmRYuGol1R88vqLfD2VR").delete()
//                                                👆
© www.soinside.com 2019 - 2024. All rights reserved.