与ViewBinder问题 [英] Problems with the ViewBinder

查看:96
本文介绍了与ViewBinder问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要显示一个的ListView 有一些图像,我用下面的code:

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

    L = name.length;

    ArrayList的< tipologia> tipologieList =新的ArrayList< tipologia>();

    tipologia []提示=新tipologia [L];

    的for(int i = 0; I<升;我++)
    {
        INT兰特=新的随机()nextInt(3)。
        可用性[i] =的String.Format(%D,兰特);
        提示[i] =新tipologia(图像[I]);

    }

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

    ArrayList的< HashMap的<字符串,对象>>数据=新的ArrayList< HashMap的<字符串,对象>>();


    的for(int i = 0; I< tipologieList.size();我++){
        tipologia吨= tipologieList.get(ⅰ);

        HashMap的<字符串,对象> tipologieMap =新的HashMap<字符串,对象>();

        tipologieMap.put(形象,t.getImm());

        data.add(tipologieMap);
    }


    从字符串= {形象} [];
    INT []到= {R.id.personImage};

    SimpleAdapterMod适配器=新SimpleAdapterMod(
            getApplicationContext(),
            数据,
            R.layout.tipologia,
            从,
            至);

    adapter.setViewBinder(新ViewBinder(){

        公共布尔setViewValue(查看视图,对象数据,
                字符串textRe presentation){

            如果(数据的instanceof字符串和放大器;&安培;查看的instanceof想象,){

                                  ((IMMAGINE)视图).loadFromURL((字符串)数据);

                }
                返回true;
            }
                其他{
                    返回false;
                }
        }
    });
 

我有一个问题:在列表中有空间4行,当我向下滚动它发生的第五行的图像是相同的第一行的图像和后续图像是错误的太<。 / P>

谁能告诉我,为什么?

解决方案

那是因为第一行的观点再次用于第五行,在第一行现在已经没有更明显。问题出在你的适配器类。

getView()方法,不要重复使用 convertview ,而不是返回一个新的观点,每次。这并非是推荐的解决方案,但应作为最后的手段。

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?

解决方案

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.

这篇关于与ViewBinder问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆