When is the getView() method of ListView called?
Asked Answered
P

2

12

I'm working on ListView. I want to know when exactly getView() is called. Is it called once the adapter is set? And does the line next to "setting adapter" get called once the getView() method completes execution?

Please help me know which line gets executed once the getView() finishes execution.

That would be a great help for me.

Thanks in advance, Vaishnvai

Ptolemaist answered 17/8, 2011 at 6:29 Comment(0)
C
12

getView() is called for each item in the list you pass to your adapter. It is called when you set adapter. When getView() is finished the next line after setAdapter(myAdapter) is called. In order to debug getView() you must toggle a breakpoint on it because you can't step into getView() from setAdapter(myAdapter). getView() is also called after notifyDataSetChanged() and on scrolling.

Cleancut answered 17/8, 2011 at 6:33 Comment(3)
Thanks for you fast response When i set some values to a variable in the getView() method. They still show zero , when i print them in the line next to setAdapter() method. These variables are accessbile to the complete Activity. Can you help over itPtolemaist
hey.Once the getView() is excecuted the control doesnt come back to the statement next to setAdapter(). Infact, the control doesnt directly go to getView() once the setAdapter() is called, almost all the statements after it are getting exceuted and then the controls move to getView(). Im literaly confused with this behaviour. Please help.Ptolemaist
Hi. That means, if i have 100 items to show in the list view, then getView() method will get called 100 times?Como
C
7

To be more clear, getView() is called whenever a new item is displayed on screen, at the count of displayed items. Which means, if you have 1 million items but 15 of them fits on screen, getView is called 15 times. Whenever you scroll up/down and new items appear, getView() is called for new ones. And you should be aware of recycler mechanism, too. Which holds a template item layout for each item type, and sends in this view to getView() method as convertView parameter, so you could use it in order to prevent layout inflation.

Celestine answered 29/4, 2014 at 7:32 Comment(4)
Hi...So, as i scroll down, the getView() method gets called for each list item that becomes visible to the user? Also, initially if only 10 items can be visible on the mobile screen at a time, then in the start the getView() method gets called 10 times? Am i thinking correctly? ... Thanks.Como
And is convertView == null everytime when the getView() is called?Como
No. If template of same item type previously populated, then recycler pass this reference as convertView parameter. You should check for null and use it if it isn't null.Glycolysis
If I want getView() to be called even if Item dosen't fit on screen. What to do?Illene

© 2022 - 2024 — McMap. All rights reserved.