ViewBinder 的问题
我需要显示带有一些图像的 ListView
,并且我使用以下代码:
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;
}
}
});
我有一个问题:列表中有 4 行的空间,当我向下滚动时,图像会出现第五行的图像与第一行的图像相同,后续图像也是错误的。
有人能告诉我为什么吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那是因为第一行的视图被第五行重用,因为第一行现在不再可见。问题出在您的适配器类中。
在
getView()
方法中,不要重用convertview
,而是每次都返回一个新的视图。这不是推荐的解决方案,而是作为最后的手段使用。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 reuseconvertview
, instead return a new view everytime. This is not the recommended solution but to be used as last resort.