自定义ListView中的重复元素甚至控制convertView == null

时间:2014-07-01 16:40:32

标签: android listview

我有一个自定义ListView,它重复元素。我试图控制元素的回收,但我不确定我是否正确地做了。那是我的代码:

static class Holder {
    static TextView nombre;
    static TextView direccion;
    static RatingBar ratingBar;
}

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

    Holder holder;

    if (convertView == null) {
        LayoutInflater inflater = LayoutInflater.from(context);
        convertView = inflater.inflate(R.layout.list_item_establecimientos, null);
        holder = new Holder();
    } else {
        holder = (Holder) convertView.getTag();
    }

    Holder.nombre = (TextView) convertView.findViewById(R.id.nombre_establecimiento);
    Holder.direccion = (TextView) convertView.findViewById(R.id.direccion_establecimiento);
    Holder.ratingBar = (RatingBar) convertView.findViewById(R.id.ratingBarListaEstablecimiento);

    convertView.setTag(holder);

    Holder.nombre.setText(this.establecimientos.get(position).getNombreEstablecimiento());
    Holder.direccion.setText(this.establecimientos.get(position).getNombreVia());
    Holder.ratingBar.setRating(this.establecimientos.get(position).getPuntuacionMedia());

    return convertView;
}

我的问题是即使照顾convertView == null(或convertView!= null)我在ListView中重复多次元素。有什么想法吗?

2 个答案:

答案 0 :(得分:2)

使用静态数据似乎是您的问题。每个类只有一个静态类成员实例。所以改变

static lass Holder { // all fields are static.  Thus only one instance exists
    static TextView nombre;
    static TextView direccion;
    static RatingBar ratingBar;
}

class Holder { // non-static fields exist in each Holder object instance
    TextView nombre;
    TextView direccion;
    RatingBar ratingBar;
}

并更改

的所有实例
Holder.property.set...

holder.property.set...

答案 1 :(得分:0)

像这样修改类。

private static class Holder {
    private TextView nombre;
    private TextView direccion;
    private RatingBar ratingBar;
}

// And make sure that you use the holder pattern correctly. It has to be for one time binding only. You are binding the views for every getView call. So that is not proper. I think static class is not an issue, because we actually need only one instance of the class.

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

    if (convertView == null) {
        view = View.inflate(context,R.layout.list_item_establecimientos, null);
        Holder holder = new Holder();
        holder.nombre = (TextView) view.findViewById(R.id.nombre_establecimiento);
        holder.direccion = (TextView) view.findViewById(R.id.direccion_establecimiento);
        holder.ratingBar = (RatingBar) view.findViewById(R.id.ratingBarListaEstablecimiento);
        view.setTag(holder);
    } else {
        view = convertview;
    }

    Holder holder = (Holder) view.getTag();
    holder.nombre.setText(this.establecimientos.get(position).getNombreEstablecimiento());
    holder.direccion.setText(this.establecimientos.get(position).getNombreVia());
    holder.ratingBar.setRating(this.establecimientos.get(position).getPuntuacionMedia());

    return view;
}