简体   繁体   English

当我在 recyclerview 中删除一个项目然后添加一个新项目时,我删除的项目再次出现在我的 Android 应用程序中。我该如何修复它? 有什么解决办法吗?

[英]When I delete an item in recyclerview and then add a new item, the items I deleted appear again in my Android App.How can I fix it? Any solution?

When I delete an item in recyclerview and then I add a new item, the items I deleted appear again on my Android app.当我在 recyclerview 中删除一个项目然后添加一个新项目时,我删除的项目再次出现在我的 Android 应用程序中。 I used the init in my Adapter to call the setonClickLister.我在我的适配器中使用了 init 来调用 setonClickLister。 I think that I need to call this code in my Activity but how will ı do ?我认为我需要在我的 Activity 中调用此代码,但我该怎么做? I use the kotlin and how can ı fix it ?我使用 kotlin,我该如何解决? I shared my adapter code below to see clearly, thanx我在下面分享了我的适配器代码以清楚地看到,thanx

class NoteAdapter(private var titleText: ArrayList<String>, private var imageButton: ArrayList<String>, private var noteText: ArrayList<String>) : RecyclerView.Adapter<NoteAdapter.ViewHolder>() {


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

        val itemTitle : TextView = itemView.findViewById(R.id.recyclerTitleText)
        val itemImage : ImageView = itemView.findViewById(R.id.recyclerImage)
        val itemDelete : ImageView = itemView.findViewById(R.id.delete)


        init {

            itemView.setOnClickListener { v: View ->


               // Toast.makeText(itemView.context,"You clicked on item # ${position + 1}", Toast.LENGTH_SHORT).show()
                val intent = Intent(itemView.context, PastNotesActivity::class.java)
                intent.putExtra("oldTitle", titleText[position])
                intent.putExtra("oldNote", noteText[position])
                intent.putExtra("oldImage", imageButton[position])
                itemView.context.startActivity(intent)

            }

            itemDelete.setOnClickListener { v: View ->

                titleText.removeAt(position)
                imageButton.removeAt(position)
                notifyItemRemoved(position)

            }
        }
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {

        val v = LayoutInflater.from(parent.context).inflate(R.layout.recycler_row, parent, false)
        return ViewHolder(v)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        holder.itemTitle.text = titleText[position]
        Picasso.get().load(imageButton[position]).resize(150,150).into(holder.itemImage)

    }

    override fun getItemCount(): Int {

        return titleText.size
    }

    

}

by the way, ı tried this code, but it does not work顺便说一句,我试过这段代码,但它不起作用

notifyItemRemoved(position)
notifyItemRangeChanged(position, titletext.size)

Put button click inside onBindViewHolder:将按钮单击内部 onBindViewHolder:

 override fun onBindViewHolder(holder: ViewHolder, position: Int) {

    holder.itemTitle.text = titleText[position]
    Picasso.get().load(imageButton[position]).resize(150,150).into(holder.itemImage)

     val itemDelete : ImageView = itemView.findViewById(R.id.delete)

     itemDelete.setOnClickListener { v: View ->

            titleText.removeAt(position)
            imageButton.removeAt(position)
            notifyItemRemoved(position)

        }

}

I have some suggests:我有一些建议:

-You shouldn't use inner ViewHolder like that. - 你不应该像那样使用内部 ViewHolder。

-And clarify your activity, adapter and viewholder. - 并澄清您的活动、适配器和查看器。

-Create your own Listener or a callback function to handle the action from the viewholder. - 创建您自己的侦听器或回调函数来处理来自查看器的操作。

-Create your data class/map/... to manage or store your data, shouldn't use like that. - 创建您的数据类/地图/...来管理或存储您的数据,不应该那样使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我在 Android Studio 上单击我的 recyclerView 项目时,如何添加访问过的背景或阴影? - How can I add visited backround or shadow when I clicked on my recyclerView item on Android Studio? 添加新项目时如何保留我的 listView 项目? - how can i keep in my listView item when i add a new item? Android studio:从数据库中删除项目时,如何更新回收站视图中的文本视图? - Android studio: How do I update a textview in a recyclerview when an item from the database is deleted? 没有数据或连接失败时,如何让我的recyclerview自动为自己添加项目? - How can I let my recyclerview auto add itself an item when there's no data or the connection failed? 当我在Listview中添加新项目时,先前的值将被删除 - When I add a new item in Listview previous values ​​are deleted 当我滚动或添加新项目时,RecyclerView数据将移至其他项目 - RecyclerView data move to other items when I scroll or add new item 当我在 Kotlin 的表(带房间)中添加或删除项目时,我的 recyclerview 会重置 - My recyclerview resets when I add or delete an item from a Table (with Room) in Kotlin 单击 RecyclerView 项目时如何将项目添加到 String ArrayList? - How do I add items to String ArrayList when I click on RecyclerView item? 是否有任何方法可以调用 SecondActivity 在具有 Recyclerview 的 FirstActivity.Fragment 上的 Arraylist 上添加新项目? - Is there any method that i can call on SecondActivity to add a new item on Arraylist on FirstActivity.Fragment that has Recyclerview? 如何修复 Recyclerview 项目删除显示错误 - How can I fix Recyclerview item deletion display error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM