Android ListView显示错误的位置

时间:2016-12-25 13:37:57

标签: java android xml listview

我对ListView有疑问。这里是适配器的代码:

public class AdapterListViewEncyclopedia extends ArrayAdapter<Apple> {

    private int layoutResourceId;
    private Context context;
    private List<Apple> data;
    private CellHolder holder;

    public AdapterListViewEncyclopedia(Context context, int layoutResourceId, List<Apple> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }


    static class CellHolder
    {
        ImageView imageApple;
        TextView tvCellAppleName;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        View row = convertView;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new CellHolder();
            holder.imageApple = (ImageView) row.findViewById(R.id.imageApple);
            holder.tvCellAppleName = (TextView)row.findViewById(R.id.tvAppleName);

            ImageLoader imageLoader = ImageLoader.getInstance();
            imageLoader.displayImage(InterfaceConfig.serverURL+data.get(position).getImageURL(), holder.imageApple);


            holder.tvCellAppleName.setText(data.get(position).getName());
            System.out.println(position);
            row.setTag(holder);
        }
        else
        {
            holder = (CellHolder)row.getTag();
        }

        return row;
    }

}

主要问题:

1.我在ArrayAdapter<Apple>中有10个数据,当我用ListView打开页面并且我用System.out.println打印位置代码时,代码显示0,1,2,3,4 ,5,0 2.当我滚动ListView时,隐藏的顶部和底部是随机位置。

任何人都可以帮我解决问题吗?

1 个答案:

答案 0 :(得分:2)

我已经更新了代码请试试这个。您必须在if-else块之后设置数据,否则在绑定数据时无法在listview中获得正确的位置。请参考android视图持有者属性 ViewHolder

public class AdapterListViewEncyclopedia extends ArrayAdapter<Apple> {

private int layoutResourceId;
private Context context;
private List<Apple> data;
private CellHolder holder;

public AdapterListViewEncyclopedia(Context context, int layoutResourceId, List<Apple> data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}


static class CellHolder
{
    ImageView imageApple;
    TextView tvCellAppleName;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
    View row = convertView;

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new CellHolder();
        holder.imageApple = (ImageView) row.findViewById(R.id.imageApple);
        holder.tvCellAppleName = (TextView)row.findViewById(R.id.tvAppleName);
        row.setTag(holder);
    }
    else
    {
        holder = (CellHolder)row.getTag();
    }
    // you can set the data here 
    setData(position,holder);

    return row;
}
private void setData(int position, CellHolder holder)
{
    ImageLoader imageLoader = ImageLoader.getInstance();
    imageLoader.displayImage(InterfaceConfig.serverURL+data.get(position).getImageURL(), holder.imageApple);

    holder.tvCellAppleName.setText(data.get(position).getName());
}
}