我正在为列表视图执行lazyload代码,在该代码中,我在json中获取文本和图像url,并将它们放入列表视图中.
图像和文本都相应显示为我想要的.
我面临的问题是当列表向下或向上滚动时,视图的索引受到干扰.
假设我的列表中有10个元素,其中图像横向预览.最初,我可以看到4个元素的onclick效果很好,
但是,当我向下滚动并单击第7个或第8个元素时,索引会受到干扰并导致空指针异常.
l2.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
View curr = parent.getChildAt((int) id);
TextView c = (TextView)curr.findViewById(R.id.text);
String sel_item = c.getText().toString();
Intent intent = new Intent(SubCatListActivity.this,GridActivity.class);
adapter.imageLoader.clearCache();
intent.putExtra("gall", sel_item);
intent.putExtra("cate", cate);
startActivity(intent);
Toast.makeText(SubCatListActivity.this,sel_item, Toast.LENGTH_SHORT).show();
}});
问题似乎与textview有关,因为我无法在列表的onclick上获得所需的文本.
如果我触摸第6个元素,则会得到第7或第8个项目的文本.
解决方法:
您可以尝试将此行更改为
TextView c = (TextView) view.findViewById(R.id.text);
并删除第一行View curr = parent.getChildAt((int)id);
l2.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
TextView c = (TextView) view.findViewById(R.id.text);
String sel_item = c.getText().toString();
Intent intent = new Intent(SubCatListActivity.this,GridActivity.class);
adapter.imageLoader.clearCache();
intent.putExtra("gall", sel_item);
intent.putExtra("cate", cate);
startActivity(intent);
Toast.makeText(SubCatListActivity.this,sel_item, Toast.LENGTH_SHORT).show();
}});