Hello!

I make a ListView output, everything is fine until the moment ListView becomes long and a scroll appears.

The screen fits 13 lines, I scroll further and the elements begin to repeat: alt text

After 13 it should go on to 30.

In Adapter, it looks like this:

@Override public View getView(int position, View convertView, ViewGroup parent) { View v; String str = items.get(position); if (convertView == null) { v = LayoutInflater.from(contextAdapter).inflate(R.layout.test_item, parent, false); TextView tvText = (TextView) v.findViewById(R.id.test_item_text); TextView tvText2 = (TextView) v.findViewById(R.id.test_item_text_2); if (tvText != null) tvText.setText("str:" + str); if (tvText2 != null) tvText2.setText("position:" + position); } else { v = convertView; } return v; } 

If I bring out from if piece with the search for elements in the layout, then everything will be almost fine:

  @Override public View getView(int position, View convertView, ViewGroup parent) { View v; String str = items.get(position); if (convertView == null) { v = LayoutInflater.from(contextAdapter).inflate(R.layout.test_item, parent, false); } else { v = convertView; } TextView tvText = (TextView) v.findViewById(R.id.test_item_text); TextView tvText2 = (TextView) v.findViewById(R.id.test_item_text_2); if (tvText != null) tvText.setText("str:" + str); if (tvText2 != null) tvText2.setText("position:" + position); return v; } 

alt text

But the problem is that every time I scroll through the list, it rebuilds ALL the elements visible on this screen. How to make it so that he would render the item 1 time, and then take it from the "saved"?

  • one
    And what is the problem to cache? You do not know how to do this, or where to keep the saved items? - VladD
  • @VladD - yes, I don’t know, that’s probably what the community wanted to know. It’s more like this: the idea of ​​caching came to me, of course, but it would have turned out to be a bicycle, Roman Zakharov’s answer - that’s probably what you need! - Opalosolo
  • one
    @ ua6xh: well, if the problem with storage and lifetime is solved, then get a Hashtable<int, View> , before creating, see if there is a hashtable, and after creation, add to the same hashtable. Standard cache, in general. - VladD
  • problem in the string v = convertView; After it you need to insert the correct initialization of elements. It just leads to duplication. - KoVadim
  • 2
    to @VladD> then get Hashtable <int, View> this is very bad advice. Highly. You cannot cache view for foliage yourself. - KoVadim

3 answers 3

read about the ViewHolder pattern here (paragraph 6.3. Holder Pattern)

    So I want to say how in the famous fable ...

    In getView() , the concept of tags should be used, that is, in the created View it is necessary to push so-called. tag / object via View.setTag() and when a non-zero convertView extract the object from there via View.getTag() and set the required values ​​to the object (in this case, widgets). Then it will not be necessary to recreate the widgets each time or vice versa to work with one widget that will split, triplicate.

    See, for example , chapter 6.4.

    PS Yes, @Roman Zakharov says the same and writes about ...

      In my case, I would advise not to read these answers. Throw away all that you have done and make a ListView for this example .