I have an ArrayAdapter subclass called TestListAdpater whose constructor is the following:
public TestListAdapter(Context context,int viewResourceId, List<TestObjects>
objects) {
super(context, viewResourceId, objects);
iHostViewID = viewResourceId;
}
and whose getView method is the following:
@Override
public View getView(int pos, View convertView, ViewGroup parent){
View itemView = convertView;
LayoutInflater inflater =
(LayoutInflater)hContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = inflater.inflate(iHostViewID, null);
TextView testTV =
(TextView)itemView.findViewById(R.id.widget_listitem_test_TV);
testTV.setText("Testing...");
}
I then add 20 of these test items to the TestListAdapter, enough that I can scroll up and down through them. After a few cycles through all of them scrolling down to the bottom and then back up to the top, I receive an OutOfMemory exception and the app crashes. My questions are three:
Why is this occurring the first place? Doesn't ArrayAdapter keep views that have already been rendered in RAM so it doesn't have to reallocate anything when getView() is called for a view that has already been displayed at least once?
How can I absolutely prevent this from ever happening, no matter how frequently the user scrolls the listview?
Does LayoutInflater::inflate() always allocate new resources?
I have a feeling from looking at the source for Android's Adapter interface that the problem is caused by my always inflating a view in getView(); Adapter's comments claim that the argument convertView is "The old view to reuse, if possible". My real use case for the custom ArrayAdapter requires an override of getView(), so how can I implement the check for re-usability that Android's default ArrayAdapter apparently performs? (I didn't see any such check in ArrayAdapter's source after a cursory look)