0

I was working through a tutorial, and I am getting following warning in Android Studio: "Unconditional layout inflation from view adapter: Should use View Holder pattern (use recycled view passed into this method as the second parameter) for smoother scrolling." on inflater.inflate(R.layout.animal_ticket, null) line.

How do I fix this? this was never explained in the course, and I cannot figure it out on my own. Any help is very much appreciated!

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
    val view : View = LayoutInflater.from(parent!!.context).inflate(R.layout.custom_layout,parent,false)

    val animalName : TextView = view.findViewById(R.id.textView2)
    val animalImage : ImageView = view.findViewById(R.id.imageView1)

    animalName.text = nameList[position]
    animalImage.setImageResource(imageList[position])

    return view
}
sam_1234
  • 117
  • 12
  • did you try the solution suggested here? - https://stackoverflow.com/questions/25381435/unconditional-layout-inflation-from-view-adapter-should-use-view-holder-patter – Daniel Knauf May 15 '22 at 21:41

1 Answers1

0

I'm assumign this is from a ListView or similar. The idea of a list view is to recycle views and reuse them. As such, you inflate them only if necessary. A view is passed in- convertView. You should only inflate a new view if convertView is not null. If convertView is null, you should instead use that view, which will be the same layout as initially inflated in a previous call.

Really you should drop ListView and replace it with RecyclerView, which is better maintained and enforces this pattern better. ListView has been if not outright deprecated, then discouraged for most of a decade.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • its from a gridView, sorry i should've included that in my description. would this advice still apply? – sam_1234 May 15 '22 at 22:10
  • Yes, a grid view is just a 2d listview. And a recyclerview replaces both of them, you'd just use a GridLayoutManager instead of a LinearLayoutManager. – Gabe Sechan May 15 '22 at 22:30