Problems with the ViewBinder

断了今生、忘了曾经 提交于 2019-12-08 07:24:38

问题


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:


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.



来源:https://stackoverflow.com/questions/5077884/problems-with-the-viewbinder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!