0

i use ListView to display a list of 30 products.

protected class ListProductAdapter extends ArrayAdapter<Product> 
        implements View.OnClickListener {
    ...
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

I noticed that when I display the activity, it first calls getView 6 times, then if I scroll down, getView will be called to display other products, but if then I scroll up, getView is called again on product for which getView had already been called. Isn't it possible to get it in memory to not have to recalculate everything I scroll down and up ?

user2016483
  • 583
  • 1
  • 5
  • 13

2 Answers2

2

The ListView recycles its Views precisely so you do not have to have a View for every item in memory all the time, but it passes an old View as convertView so it can be reused and doesn't have to be re-inflated every time. See here.

Community
  • 1
  • 1
ci_
  • 8,594
  • 10
  • 39
  • 63
1

That's how ListView works. It manages a number of views and calls getView(int, View, ViewGroup) whenever it needs one view to be set up. It doesn't hold to many views though. You might be able to hold all in memory, but that's not its intent.

If convertView is null the list needs you to create the views (your first 6 calls to getView). When you start scrolling, the list will provide you with an (already initialized) convertView which you only need to refill with the data you want to display.

See the documentation of getView for an elaborate explanation.

tynn
  • 38,113
  • 8
  • 108
  • 143