android – Yet another getView called multiple times-ThrowExceptions

Exception or error:

Each item in my ListView containt an ImageView and a TextView, this will be filled with remote information.

I got an URL for the ImageView, therefore I start an AsyncTask which downloads the image and will call setImageBitmap with the downloaded bitmap.

This goes very well but when the ListView is created, the getView() is called to often.
It calls about 7 times the getView for the first 10 rows (only 7 visible). (So: 0, 1, etc, 10, 0, 1 etc).

Then I can just scroll smoothly to the 10th item. But after that, for each new row the listview calls again about 7 times the getView for the first 10 items. (This will cause lag..)

But when I remove the setImageBitmap from the AsyncTask, this all won’t happen!

What could be the problem? Could it be that some layout is to big which will cause another streak of getViews ?

Here some code:

<ListView
    android:id="@+id/mylist"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true"
    android:layout_below="@+id/mydivider"
    android:divider="@+color/mycolor"
    android:dividerHeight="2dp"
    android:fadingEdge="none"
    android:fastScrollEnabled="true"
    android:listSelector="@android:color/transparent"
    android:scrollbars="horizontal"
    android:scrollingCache="false"
    android:smoothScrollbar="false" />

The AsyncTask:

public static class MyTask extends AsyncTask<String, Integer, Bitmap> {
    private LruCache<String, Bitmap> mMap;
    private String mUri;
    private int mPosition;
    private ViewHolder mHolder;

    public MyTask (ViewHolder holder, int position, LruCache<String, Bitmap> map) {
        mMap = map;
        mHolder = holder;
        mPosition = position;
    }

    @Override
    protected Bitmap doInBackground(String... url) {
        mUri = url[0];
        return getBitmapFromURL(mUri);
    }

    @Override
    protected void onPostExecute(Bitmap b) {
        if (b != null) {
            mMap.put(mUri, b);
            if (mPosition == mHolder.position) {
                holder.image.setImageBitmap(b);
            }
        }
    };
}

getView()

row = convertView;
ViewHolder holder;
if (row == null) {
    row = vi.inflate(R.layout.mylayout, null);
    holder = new ViewHolder();
    holder.image = (ImageView) row.findViewById(R.id.myimage);
    holder.title = (TextView) row.findViewById(R.id.mytext);
    row.setTag(holder);
} else {
    holder = (ViewHolder) row.getTag();
}

holder.title.setText("text");
holder.image.setImageResource(R.drawable.mydefaulticon);
holder.position = position;
startMyTask(content, holder, position);

Some more information:

When an new view is created the stacktrace shown the getView was called from ListView.makeAndAddView()
But in the useless streak of getViews it is coming from ListView.measureHeigthOfChildren()

So it seems like the layout is changed when I set the Bitmap…

How to solve:

The problem was in the Layout of the ListView.

The parameter layout_width was set to wrap_content when I changed it to fill_parent the problem disappeared…

###

i recommend you to watch this video http://www.youtube.com/watch?v=wDBM6wVEO70

basically android will call getView multiple times for it’s calculation of view heights and stuff, so you need to implement some sort of cache in your async, check http://code.google.com/p/android-query/ which has that if you don’t use authentication

###

The problem is that your ListView does not know what the of each item is. When initializing, the ListView will measure all of its children and draw itself upon this information. What you are doing with your AsyncTask, is changing the sizes of the items in the list and not letting the ListView draw itself again completely. This results in your ListView to behave strangly.

Solution: You should update your ListView by notifying the Adapter using notifyDataSetChanged() in the PostExecute of your AsyncTask. This will allow the ListView to draw itself with the new information.

I hope it works !

A few helpfull links:

http://thinkandroid.wordpress.com/2012/06/13/lazy-loading-images-from-urls-to-listviews/

Lazy load of images in ListView

###

Sorry for late reply. I had been facing the same problem in my adapter for gallery widget. I had employed Universal Image Loader. Martijn’s solution worked for me. What all I had to do was to add notifyDataSetChanged() when the image had been completely downloaded and imageView was being set.

###

I tried using fill_parent as height in ListView but i was unable to achieve it perfect but how ever i tried it by preventing creating convertViews if it its already exist. it works fine in my appoach.

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
         if (convertView != null)
            return convertView;
         else {
            //your code...
         }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *