1

I need to display a ListView with some images, and I'm using the following code:

    dataText = (TextView)findViewById(R.id.data);
    dataText.setText(camping.stringIn + " >> " + camping.stringOut);

    l = name.length;

    ArrayList<tipologia> tipologieList=new ArrayList<tipologia>();

    tipologia[] tip = new tipologia[l];

    for (int i = 0; i < l; i++) 
    {
        int rand = new Random().nextInt(3);
        availability[i] = String.format("%d", rand);
        tip[i] = new tipologia(image[i]);

    }                      

    for(int i=0; i<tip.length; i++) 
    {
        tipologieList.add(tip[i]);
    }

    ArrayList<HashMap<String, Object>> data=new ArrayList<HashMap<String,Object>>();


    for(int i=0;i<tipologieList.size();i++){
        tipologia t = tipologieList.get(i);

        HashMap<String,Object> tipologieMap = new HashMap<String, Object>();

        tipologieMap.put("image", t.getImm()); 

        data.add(tipologieMap);  
    }


    String[] from={"image"};
    int[] to={R.id.personImage};

    SimpleAdapterMod adapter=new SimpleAdapterMod(
            getApplicationContext(),
            data,
            R.layout.tipologia,
            from,
            to);

    adapter.setViewBinder(new ViewBinder() {

        public boolean setViewValue(View view, Object data,
                String textRepresentation) {

            if(data instanceof String && view instanceof Immagine ){

                                  ((Immagine)view).loadFromURL((String)data);

                }
                return true;
            }
                else{
                    return false;
                }
        }
    });

I have a problem: in the list there's space for 4 rows, and when I scroll down it happens that the image of the fifth row is the same of the image of the first row and the subsequent images are wrong too.

Can someone tell me why?

user
  • 86,916
  • 18
  • 197
  • 190
Abramodj
  • 5,709
  • 9
  • 49
  • 75

1 Answers1

3

Thats because the first row's view is reused for the fifth row, as the first row is now no more visible. The problem is in your adapter class.

In getView() method, do not reuse convertview, instead return a new view everytime. This is not the recommended solution but to be used as last resort.

Ron
  • 24,175
  • 8
  • 56
  • 97
  • Awarded bounty and voted up because it seems to answer OP (unfortunately not my problem, but I think I've worked out how to fix that anyway). :) – hrickards Aug 22 '11 at 15:51