Memory leak in adapter array

To do this quickly, I'll explain my implementation and then the problem =):

I have an object:

Class myClass{
  ......attrs......
  ......getters and setters......

  public void setOnMyClassChangeListener(MyClassChangeListener listener){
      this.listener=listener;
  }
}

      

And ListView controlling objects of this class with this method in it ArrayAdapter:

static class ViewHolder {
        View view1,view2,view3.....;
    }

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

    final ViewHolder holder;
    View rowView = convertView;
    if (rowView == null) {
        LayoutInflater inflater = (LayoutInflater) getContext()
             .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(R.layout.templaterow, null, true);
        holder = new ViewHolder();
        holder.view1=.....;
        holder.view2=.....;
        holder.view3=.....;
        rowView.setTag(holder);
    } else {
        holder = (ViewHolder) rowView.getTag();
    }
    MyClass object= items.get(position)
    object.setOnMyClassChangeListener(MyClassChangeListener(){
          public void onMyClassChangeListener(MyClass obj){
               holder.view1.setXX(obj.getValue1());
               holder.view2.setXX(obj.getValue2());
               ......
          }
    }
    );
}

      

So, I think I am wasting memory because the listener stays set after the ListActity exits and then the owner leaked out correctly?

Is there a better way than retrying the Adapter transition to onStop by removing the listeners?

thank

+3


source to share


1 answer


Yes, there is a better method: don't set such a listener on your object that has views references.



Instead, you just modify the model (MyClass) and then call the adapter .notifyDatasetChanged () on the ArrayAdapter. Then the ListView will be redrawn.

0


source







All Articles