On Wed, Oct 20, 2010 at 10:09 PM, Aaron <[email protected]> wrote:
> I am working with a ListView, custom adapter and a large number of
> items.  I read in a book for Android that is was more efficient to use
> what it called the holder pattern.  That is to create a wrapper class
> for each view in the list view that cached the objects in the view so
> as to avoid calls to findViewById because those are supposed to be
> expensive.  Well anyway I tried this and I saw a lot, I mean, A LOT of
> garbage collection happening.  This is obviously no real surprise.

It's surprising to me. Part of the point is to avoid allocations.

> My
> question is what is better?  To have 50,000 objects GC'd every time
> the user scrolls or to make the 4 or five calls to findViewById per
> view?

The better answer is "neither".

> Below is my implementation of what the book suggested.
>
>       �...@override
>        public View getView(int position, View convertView, ViewGroup parent)
> {
>                View view = convertView;
>                if (view == null) {
>                        final LayoutInflater inflater = (LayoutInflater)
> context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
>                        view = inflater.inflate(R.layout.survey_item, null);
>                        view.setTag(new SurveyItemWrapper(view));
>                }
>                bindView((SurveyItemWrapper) view.getTag(), position);
>                return view;
>        }
>
>        private void bindView(SurveyItemWrapper surveyItemWrapper, int
> position) {
>                final SurveyedItem surveyedItem = surveyedItems.get(position);
>
> surveyItemWrapper.getDescription().setText(surveyedItem.getItemName());
>                surveyItemWrapper.getCube().setText(String.format("%9.2f",
> surveyedItem.getCube()));
>                surveyItemWrapper.getShipping().setText(String.format("%d",
> surveyedItem.getShipping()));
>                surveyItemWrapper.getNotShipping().setText(String.format("%d",
> surveyedItem.getNotShipping()));
>        }
>
>
>        private class SurveyItemWrapper {
>
>                private TextView description;
>                private TextView cube;
>                private TextView shipping;
>                private TextView notShipping;
>                private View view;
>
>                public SurveyItemWrapper(View view) {
>                        this.view = view;
>                }
>
>                public TextView getDescription() {
>                        if (description == null) {
>                                description = (TextView)
> view.findViewById(R.id.SurveyItemDescription);
>                        }
>                        return description;
>                }
>
>                public TextView getCube() {
>                        if (cube == null) {
>                                cube = (TextView) 
> view.findViewById(R.id.SurveyItemCube);
>                        }
>                        return cube;
>                }
>
>                public TextView getShipping() {
>                        if (shipping == null) {
>                                shipping = (TextView) 
> view.findViewById(R.id.SurveyItemShipping);
>                        }
>                        return shipping;
>                }
>
>                public TextView getNotShipping() {
>                        if (notShipping == null) {
>                                notShipping = (TextView)
> view.findViewById(R.id.SurveyItemNotShipping);
>                        }
>                        return notShipping;
>                }
>
>        }

I strongly suspect that your GC problem is in String.format(), not in
the findViewById() caching. There are no allocations being done in the
row recycling case, other than simple setText() calls and
String.format() (plus anything that might be in your model getters).

FWIW, the next edition of the book you have will use a simpler holder class.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

Android App Developer Books: http://commonsware.com/books

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to