0

I have java.lang.IndexOutOfBoundsException while implementing filter for recyclerView. Error occurs in onBindViewHolder method, and the position value is bigger than filtered list, so it makes indexoutofboundException. I don't know why position value is bigger than filteredlist eventhough I made notifyDataSetChanged() call. This is my code for RecyclerView adapter. Please give me some solution to overcome this problem. Thank you!!

class MainListAdapter(
private val context: Context,
private val item_list: MutableList<items_list>) :
RecyclerView.Adapter<MainListAdapter.ViewHolder>(),
Filterable {
var filteredList: MutableList<items_list>? = item_list

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

    val Photo = view.PhotoImg
    val item_name = view.item_name

    fun bind(item: items_list, context: Context) {
        val resourceId =
            context.resources.getIdentifier(item.photo, "drawable", context.packageName)
        Photo.setImageResource(resourceId)
        item_name.text = item.item_name
    }
}

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

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val current = filteredList?.get(position)
    if (current != null) {
        holder.bind(current, context)
    }

}

override fun getItemCount(): Int = item_list!!.size

override fun getFilter(): Filter? {
    return object : Filter() {
        override fun performFiltering(constraint: CharSequence): FilterResults {
            val charString = constraint.toString()
            filteredList = if (charString.isEmpty()) {
                item_list
            } else {
                val filteringList = ArrayList<items_list>()
                if (item_list != null) {
                    for (name in item_list) {
                        if (name.item_name.toLowerCase().contains(charString.toLowerCase())) {
                            filteringList.add(name);
                        }
                    }
                }
                filteringList
            }
            val filterResults = FilterResults()
            filterResults.values = filteredList
            return filterResults
        }

        override fun publishResults(constraint: CharSequence, results: FilterResults) {
            filteredList = results.values as ArrayList<items_list>
            notifyDataSetChanged()
        }
    }
}

}

jmmjm
  • 1
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Nov 16 '21 at 04:26

1 Answers1

0

Its because your getCount returns item_list.size, but you're indexing the position into filtered_list, which is a subset of item_list. getCount should return the size of the filtered list, not the original list. By returning the size of the original list, which is bigger, you make the recycler list try to show items that are beyond the bounds of the filtered list and cause it to throw this error.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127