Problems with the ViewBinder

361 views Asked by At

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?

1

There are 1 answers

1
Ron On

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.