0

How to refer to adapter in the getview (of a listview) with this google syntax :

fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
    return (convertView ?: layoutInflater.inflate(R.layout.my_layout, parent, false)).apply {
        // … bind content from position to convertView …
        //personal comment : "this" does not refer to adapter
    }
}

Indeed, after the .apply, I have to call adapter in order to make an adapter.notifyDataSetChanged().

With a "classic syntax", adapter could be called thanks to "this", that is impossible with google syntax.

"Classic syntax"

fun getView(position: Int, convertView: View?, parent: ViewGroup): View {

....

//"this" (refers to adapter)

return convertView
}

So is it possible to overriding the getview function to add variable in the signature ? Else, what is the solution of my problem ?

jujuf1
  • 175
  • 2
  • 10

2 Answers2

0

You should use .also { ... } instead of .apply { ... }

Antoine
  • 307
  • 2
  • 13
0

If I got your question right, you can:

Write your getView(...) function same as adapters, with additional parameter

call your getView() in adapter overridden getView(...) pass your argument what you want.

Narek Hayrapetyan
  • 1,731
  • 15
  • 27
  • Thx you but when I add some parameter it is written that « the function overrides nothing. » – jujuf1 Dec 31 '20 at 07:37