开发者

Problems with the ViewBinder

开发者 https://www.devze.com 2023-02-12 08:05 出处:网络
I need to display a ListView with some images, and I\'m using the following code: dataText = (TextView)findViewById(R.id.data);

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 SimpleA开发者_如何学编程dapterMod(
            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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号