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.  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?  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;
                }

        }

-- 
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