I've been scouring throug the examples and tutorials but I can't seem to get my head around how to handle recycling within a subclassed SimpleCursorAdapter. I know that for regular ArrayAdapters you can check convertView for null and inflate if null from the xml and if not null, recycle, but I'm having a little trouble visualizing how that works with the from and to arrays within the SimpleCursorAdapter subclass. I tried to figure this out from the The Busy Coders Guide to Android Development by Commonsware but was unsuccessful. If anyone knows of any tips, examples, or tutorials, I would be grateful to see them.
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- how to split a list into a given number of sub-lis
- Listening to outgoing sms not working android
} }
CursorAdapter does the work partially for you. You only need to override newView() when a new view needs to be created, and bindView() when an existing view is recycled.
I was unable to find any good examples for Subclassing the SimpleCursorAdapter to use the convertView recycle methodolgy so I ended up subclassing CursorAdapter instead. This actually worked quite well for my implementation and is blazing fast with a dramatic decrease in memory usage. Recycling views really works and I recommend it highly!
Here is an example of my implementation:
Each row in my list now displays name, city, state, and country exactly as I wanted. Hope this helps.
Based on my testing, it appears that the accepted answer is incorrect (more so, unnecessary).
From what I can tell (based on a simple Log.e output test), view recycling is correctly handled by default (as @Romain Guy stated). Which I think would justify there being no recommendation for you to need to override getView() in any documentation I have come across.
This was the code snippet used in the simple example I used to come to this conclusion:
When using this in a GridView to display a grid of Image thumbnails available the LogCat output was the following:
This seems to show quite clearly that once the views have been created by a call to newView(), bindView() handles converting the views to optimize memory.
(The few lines where newView() is called around the middle are just the GridView requiring a new row when displaying two fractions of a row as opposed to one whole).
You could also subclass ResourceCursorAdapter. In that case you only need to override the bindview method:
}