【问题标题】:Getting an IndexOutOfBoundsException when removing the last item from my recyclerview从我的回收站视图中删除最后一项时获取 IndexOutOfBoundsException
【发布时间】:2018-01-04 06:01:31
【问题描述】:

我有一个应用程序,您可以在其中收藏项目和一个活动,您可以在其中查看您收藏的项目。在收藏活动中,您还可以删除之前收藏的项目。收藏夹活动包括一个简单的 recyclerview 来显示收藏的项目,当然还有一个相应的适配器,它接受一个收藏夹的 ArrayList 来填充 recyclerview。我还有一个 FavoritesManager 类来处理收藏夹的添加和删除。我遇到的问题是,当我在收藏夹活动中删除我的收藏夹列表中的最后一个收藏夹时,我的应用程序崩溃并且我得到一个IndexOutOfBoundsException。以下是当点击按钮从收藏夹中删除项目时运行的代码:

class FavoritesListAdapter(val context: Context, val favorites: ArrayList<FavoriteSong>) : RecyclerView.Adapter<FavoritesListAdapter.FavoriteViewHolder>() {
 override fun onBindViewHolder(holder: FavoriteViewHolder?, position: Int) {
    holder?.favoriteBtn?.setOnClickListener {
        holder.favoriteBtn?.setImageResource(R.drawable.ic_favorite_border_black_24dp)
        favorites.removeAt(holder.adapterPosition)
        notifyItemRemoved(holder.adapterPosition)
        notifyDataSetChanged()
        favoritesManger.removeFavorite(context, favorites[holder.adapterPosition])
    }
}

这是我的 FavoritesManager 类中用于从数据库中删除收藏夹的代码:

fun removeFavorite(context: Context, song: FavoriteSong) {
    val favoriteSongs = getFavorites(context)
    if (favoriteSongs != null) {
        favoriteSongs.remove(song)
        saveFavorites(context, favoriteSongs)
    }
}

我已将问题深入到调用 FavoriteManager 类以从数据库中删除收藏夹的适配器中,但我不知道为什么会导致问题。我可以使用 FavoritesManager 类从应用程序的其他地方删除最后一项,但是当我运行那段代码时,我得到了这个错误:

E/UncaughtException: java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
                                                                                at java.util.ArrayList.get(ArrayList.java:437)
                                                                                at com.example.favorites.adapter.FavoritesListAdapter$onBindViewHolder$1.onClick(FavoritesListAdapter.kt:37)
                                                                                at android.view.View.performClick(View.java:6294)
                                                                                at android.view.View$PerformClick.run(View.java:24770)
                                                                                at android.os.Handler.handleCallback(Handler.java:790)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                at android.os.Looper.loop(Looper.java:164)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

还有其他方法可以删除列表中的最后一项吗?我要删除的项目在列表的范围内并且存在于列表中,所以我不明白为什么它没有被删除。

【问题讨论】:

    标签: android arraylist android-recyclerview


    【解决方案1】:

    尝试更新的代码

     class FavoritesListAdapter(val context: Context, val favorites: ArrayList<FavoriteSong>) : RecyclerView.Adapter<FavoritesListAdapter.FavoriteViewHolder>() {
         override fun onBindViewHolder(holder: FavoriteViewHolder?, position: Int) {
            holder?.favoriteBtn?.setOnClickListener {
                holder.favoriteBtn?.setImageResource(R.drawable.ic_favorite_border_black_24dp)
                favorites.removeAt(holder.adapterPosition)
                favoritesManger.removeFavorite(context, favorites[holder.adapterPosition])
                //notifyItemRemoved(holder.adapterPosition)
                notifyDataSetChanged() 
            }
        }
    

    【讨论】:

      【解决方案2】:

      在您的原始代码中,我看到这两行:

      favorites.removeAt(holder.adapterPosition)
      ...
      favoritesManger.removeFavorite(context, favorites[holder.adapterPosition])
      

      您似乎正在按索引从favorites 列表中删除某些项目,然后尝试再次查找同一项目作为favoritesManger.removeFavorite() 的第二个参数。如果holder.adapterPosition 是列表中的最后一个索引,这将导致IndexOutOfBoundsException。 (即使它不是最后一个索引,您也会从不同的集合中删除两个不同的项目。)

      removeAt() 方法返回删除的项目,因此您应该可以将这两行替换为:

      val removed = favorites.removeAt(holder.adapterPosition)
      ...
      favoritesManger.removeFavorite(context, removed)
      

      【讨论】:

        猜你喜欢
        • 2020-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多