Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple calling of getView() in GridView

Tags:

android

My Activity consists of GridView which holds 40+ elements. After starting activity user could see maximum 15 items (3 rows, 5 items in each row). I wrote in getView() body to pass to the LogCat number of getting View:

Log.i("getView()", "GETTING VIEW_POSITION[" + String.valueOf(position) + "]" + (convertView != null?convertView.toString():"null"));

After launching my app I get such log:

02-09 14:34:56.900: INFO/getView()(388): GETTING VIEW_POSITION[0]null
02-09 14:34:57.300: INFO/getView()(388): GETTING VIEW_POSITION[0]android.widget.FrameLayout@44c7a9c0  
02-09 14:34:57.300: INFO/getView()(388): GETTING VIEW_POSITION[1]null
02-09 14:34:57.400: INFO/getView()(388): GETTING VIEW_POSITION[2]null
02-09 14:34:57.510: INFO/getView()(388): GETTING VIEW_POSITION[3]null
02-09 14:34:57.620: INFO/getView()(388): GETTING VIEW_POSITION[4]null
02-09 14:34:57.720: INFO/getView()(388): GETTING VIEW_POSITION[5]null
02-09 14:34:57.840: INFO/getView()(388): GETTING VIEW_POSITION[6]null
02-09 14:34:57.930: INFO/getView()(388): GETTING VIEW_POSITION[7]null
02-09 14:34:58.273: DEBUG/dalvikvm(388): GC freed 3530 objects / 322744 bytes in 270ms
02-09 14:34:58.280: INFO/getView()(388): GETTING VIEW_POSITION[8]null
02-09 14:34:58.300: INFO/getView()(388): GETTING VIEW_POSITION[9]null
02-09 14:34:58.320: INFO/getView()(388): GETTING VIEW_POSITION[10]null
02-09 14:34:58.340: INFO/getView()(388): GETTING VIEW_POSITION[11]null
02-09 14:34:58.360: INFO/getView()(388): GETTING VIEW_POSITION[12]null
02-09 14:34:58.380: INFO/getView()(388): GETTING VIEW_POSITION[13]null
02-09 14:34:58.400: INFO/getView()(388): GETTING VIEW_POSITION[14]null
02-09 14:34:59.220: INFO/getView()(388): GETTING VIEW_POSITION[0]null
02-09 14:34:59.490: INFO/getView()(388): GETTING VIEW_POSITION[0]android.widget.FrameLayout@44c69ef0

I also red this post about such kind of issue. But My GridView's height in XML description set as fill parent:

<?xml version="1.0" encoding="utf-8"?>

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/grid"
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent"
                android:numColumns="auto_fit"
                android:horizontalSpacing="10dp"
                android:verticalSpacing="10dp"
                android:columnWidth="140dp"
                android:gravity="center"
                android:scrollbars="none"
                /> 

And finally to remove all doubts, here is my Adapter code:

public class ImageAdapter extends BaseAdapter
    {
        private Activity activity;
        private LayoutInflater inflater = null;
        private ArrayList<Photo> photoes;
        
        public ImageAdapter(Activity a, ArrayList<Photo> pictures)
        {
            photoes = pictures;
            activity = a;
            inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            Log.i("getView()", "GETTING VIEW_POSITION[" + String.valueOf(position) + "]" + (convertView != null?convertView.toString():"null"));
                View mViewSlice = convertView;
                if(convertView == null) {
                    mViewSlice = inflater.inflate(R.layout.photo_preview, null);
                    ((TextView) mViewSlice.findViewById(R.id.name_of_photo)).setText(photoes.get(position).getName());
                    
                    mViewSlice.setPadding(5, 5, 5, 5);
                    mViewSlice.setLayoutParams(new GridView.LayoutParams(-2, -2));
                    
                }
        
               
                      
               return mViewSlice;
        }

        @Override
        public int getCount() {
            return photoes.size();
        }

        @Override
        public Object getItem(int position) {
            return photoes.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }   
    }

I hope somebody will respond at my problem and will help me to solve it.

Waiting for your suggestions. Alex.

like image 537
teoREtik Avatar asked Feb 09 '11 14:02

teoREtik


2 Answers

The response on that link if quite clear I think

There is absolutely no guarantee on the number of times getView() will be invoked for any given position

Now this remark also implies you shouldn't use wrap_content

usually occurs with lists and grids that have a height set to wrap_content

But that does not mean it would not happen for other situations. The bug was closed as "WorkingAsIntended", so I don't think you can do too much. It's just something that can happen.

like image 148
Nanne Avatar answered Oct 15 '22 13:10

Nanne


For me, it's even worse: 0 position is called often 10 times. And it's called even if I'm at the end of the list, without position 0 on the screen.

My solution is a static ArrayList of items being currently processed for display and I ignore those double-calls. It's not the best solution though as it still uses resources to get 0 position while it's even not on the screen.

As it seems like a bug, I think I will cache a View for position 0 so it would get a quick and proper return value even if the position is not on the screen.

Android 4.2.2.

like image 32
A.P. Avatar answered Oct 15 '22 13:10

A.P.