Explanation Of The Getview() Method Of An Arrayadapter
Solution 1:
Is is a cached copy of the last layout returned by getView()?
The convertView
is the view of a row that left the screen(so it isn't the last view returned by the getView
method). For example, the list is first shown, in this case convertView
is null
, no row view was previously built and left the screen. If you scroll down, row 0 will leave the screen(will not be visible anymore), when that happens the ListView
may choose to keep that view in a cache to later use it(this makes sense, as the rows of a ListView
generally have the same layout with only the data being different). The reason to keep some views in a cache and later use them is because the getView
method could be called a lot of times(each time the user scrolls up/down and new rows appear on the screen). If each time the row view would need to be recreated this would have resulted in a lot of objects being created which is something to avoid. In your getView
method you would check convertView
to see if it is null
. If it's null
then you must build a new row view and populate it with data, if it isn't null
, the ListView
has offered you a previous view. Having this previous view means you don't need to build a new row layout, instead you must populate it with the correct data, as that cached view has the old data still attached to it(you would see a lot of questions on stackoverflow where users ask why the rows of their ListView
are duplicating when they scroll down).
What does the parent parameter do. I haven't seen too many examples utilising this. Most of them simply reuse/inflate a row layout and return it.
It should be used to get the correct LayoutParams
for the newly inflated/built row. For example, if you inflate a layout which has a RelativeLayout
as the root and you don't use the parent
to get the LayoutParams
you could have some problems with the row layout. To take the parent in consideration you would use :
convertView = getLayoutInflater().inflate(R.layout.row_layout, parent, false);
Solution 2:
My understanding of convertView
is that it's essentially views that have been recycled because they're not being used at the moment - for example, you scroll down the list, the ones at the top aren't on the screen, so they get passed into this parameter for use when you need a new view (so you don't have to create a whole new one while having unused ones sitting around idle). iOS has a similar method called dequeueReusableCellWithIdentifier
. If each row of your listview has the same structure, it's safe to cast this to the appropriate type and just update the information in it - text, images, etc. It will be a View that was previously returned by a getView()
call for the same list.
My best guess (and it is admittedly a guess) with parent
is that it's the view that this adapter's list is a child of. It gives you a route back to the rendering system if you need a context, access to the resource system, to pass information to or receive information from the list's parent view.
Post a Comment for "Explanation Of The Getview() Method Of An Arrayadapter"