I’m getting Out of memory errors. I checked the DDMS and find that the places I allocate more memory is in the getView() method in my adapters. Is it a better way for me to create objects then this way:
view=new ImageView(myContext);
This is one example of my getView() method:
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView==null){
view=new ImageView(myContext);
view.setLayoutParams( new AbsListView.LayoutParams(columnWidth,columnWidth));
view.setBackgroundColor(Color.GRAY);
}else{
view=convertView;
convertView.setBackgroundColor(Color.GRAY);
}
return view;
}
I get the context in the c’tor:
public emptySquaresAdapter( Context myContext,) {
super();
this.myContext=myContext;
}
I read about context leak, is that the case?
EDIT:
In this case I show all the views in the screen at once, so the converted view is never used (I don’t need to scroll to show more views).
this adapter shows 12 squares on the screen. The method getView is called 12 times with the position 0 (I guess to measure all the child vies) and only then continues to position 1,2,3…
Why doe’s it happens? Can this make the OOM problem?
I call this adapter from another adapter, and I guess that what makes everything complicated.
I also unbind all drawables at onDestroy, and set imageView.getDrawable().setCallback(null) to all of the imageViewsIt still doesn’t work
The code you posted is ok. The problem is, probably, somewhere else.
P.S. Why do you store context object? You can get it any time you need in your getView() method by calling parent.getContext()
And to my experience, it doesn’t matter what kind of context you pass as long as you don’t hold onto that (what you are actually doing).
But if your adapter itself is not stored somewhere and is just a part of an activity it should be ok, since adapter will just get gc’ed with activity.