我有问题,我使用自定义ListView文本视图和图像查看,但当我滚动列表时,图像查看位置是更改。我不知道为什么。当第一次列表视图加载时,所有的东西都没问题,但是当我滚动时,图像视图位置就会变化。
please give me any suggestion for this.
/* this is my code */
public class CustomListAdapter extends ArrayAdapter<EntityList>
{
ArrayList<EntityList> entitiesList;
LayoutInflater vi;
int Resource;
// ViewHolder holder;
ImageView globalImgView;
private Context context;
public CustomListAdapter(Context context, int resource, ArrayList<EntityList> objects) {
super(context, resource, objects);
// this.context = context;
vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resource = resource;
entitiesList = objects;
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent)
{
final ViewHolder holder;
View v = convertView;
if (v == null)
{
holder = new ViewHolder();
v = vi.inflate(Resource, null);
holder.tvName = (TextView) v.findViewById(R.id.tvName);
holder.tvimgview = (ImageView) v.findViewById(R.id.img_mobicon);
String listName = entitiesList.get(position).getListName();
Map<String, Bitmap> entityListImgName = HttpHandler.getInstance().getEnitityListImgMap();
if(entityListImgName.containsKey(listName))
{
Bitmap bmp = entityListImgName.get(listName);
holder.tvName.setText(listName);
holder.tvimgview.setImageBitmap(bmp);
}
v.setTag(holder);
}
else
{
holder = (ViewHolder) v.getTag();
}
try
{
String listName = entitiesList.get(position).getListName();
holder.tvName.setText(listName);
}
catch(Exception ex)
{
System.out.println("Exception in CustomListAdapter class :" +ex);
}
return v;
}
static class ViewHolder
{
public TextView tvName;
public ImageView tvimgview;
}
}
答案 0 :(得分:0)
您的问题(可能)就是,v == null
时,您正在为持有者提供价值。我参考你代码的这一部分
String listName = entitiesList.get(position).getListName();
Map<String, Bitmap> entityListImgName = HttpHandler.getInstance().getEnitityListImgMap();
if(entityListImgName.containsKey(listName))
{
Bitmap bmp = entityListImgName.get(listName);
holder.tvName.setText(listName);
holder.tvimgview.setImageBitmap(bmp);
}
使用持有人时,您只需使用findViewById
定义视图,或者在视图为空时插入将为所有行修复的值。
将该部分代码移至或复制到else
区块,并像ImgView
一样填充TextView
,并在滚动时刷新您的回收视图。
答案 1 :(得分:0)
String listName = entitiesList.get(position).getListName();
Map<String, Bitmap> entityListImgName = HttpHandler.getInstance().getEnitityListImgMap();
if(entityListImgName.containsKey(listName))
{
Bitmap bmp = entityListImgName.get(listName);
holder.tvName.setText(listName);
holder.tvimgview.setImageBitmap(bmp);
}
将此代码放在tvName.settext
之后