简体   繁体   中英

Is it possible to add a variable in the getview adapter function by overriding it?

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM