1

网络上可能有数千篇关于 ListView 和 ListAdapter 回收过程的帖子,但到目前为止我无法解决我的问题。

我有自定义 ListView 行,并使用从远程服务器获得的数据填充行内的项目(TextViews、ImageViews 等)。屏幕一次最多可以显示 5 行,如果我有更多行,我必须滚动。问题是; 适配器仅存储和回收前 5 行的信息,即使我滚动查看第 6、7、8 等行,我也会在随机位置一次又一次地看到相同的 5 行。

这是代码:

public class ListAdapter extends BaseAdapter {

    private ArrayList<String> id, name, address;
    private Context context;

    public ListAdapter(Context c, ArrayList<String> id,
            ArrayList<String> name, ArrayList<String> address,) {
        this.id = id;
        this.name = name;
        this.address = address;
        this.context = c;
    }

    @Override
    public int getCount() {
        return id.size();
    }

    @Override
    public Object getItem(int position) {
        return id.get(position);
    }

    @Override
    public long getItemId(int id) {
        return id;
    }

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

        if (convertView == null) {
            rowsLayout = (View)LayoutInflater.from(context)
                           .inflate(R.layout.list_item, null);
            //Assign values to TextViews in here
        } else {
            rowsLayout = convertView;
        }
            return rowsLayout;
    }
}

我尝试使用此网站中的方法,但没有帮助。

如果有人可以为我提供解决方案(请提供代码)或将我引导到另一篇文章,我将不胜感激。

4

1 回答 1

0

从此更改您的代码:

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

    if (convertView == null) {
        rowsLayout = (View)LayoutInflater.from(context)
                       .inflate(R.layout.servisler_list_item, null);
        //Assign values to TextViews in here
    } else {
        rowsLayout = convertView;
    }
        return servisRowsLayout;
}

对此:

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

    if (convertView == null) {
        rowsLayout = (View)LayoutInflater.from(context)
                       .inflate(R.layout.servisler_list_item, null);
    } else {
        rowsLayout = convertView;
    }

        //Assign values to TextViews in here

        return servisRowsLayout;
}

您需要在每次 getView()调用时“将值分配给 TextViews”,而不仅仅是其中一些。

于 2013-05-13T00:55:47.857 回答