Custom CursorAdapater's bindView called 77 times...have I done something wrong?
Asked Answered
C

2

3

I read this question where it says not to worry about it but I guess I need some reassurance.

My custom CursorAdapter's bindView:

@Override
public void bindView(View view, Context context, Cursor c) {
    // get handles for views in xml
    ImageView imageView = (ImageView)view.findViewById(R.id.add_lvrow_image);
    TextView titleView = (TextView)view.findViewById(R.id.add_lvrow_title);

    // get data from cursor and "massage" if necessary
    String imageUri = c.getString(c.getColumnIndex(CollectionsTable.COL_IMAGEURI));
    String title = c.getString(c.getColumnIndex(CollectionsTable.COL_TITLE));

    // terrible time getting run-time sizes of imageView, hardcode for now
    int XML_WIDTH = 100;
    int XML_HEIGHT = 100;       

    Log.d(TAG, SCOPE + "bindView called: " +count);
    count++;

    // use static util class
    ImageUtils.loadBitmap(context, imageUri, imageView, XML_WIDTH, XML_HEIGHT);

I'm following the series of Android tutorials for loading large bitmaps but have moved decodSmapledBitmapFromUri, calculateInSmapleSize, loadBitmap, BitmapWorkerTask, AsyncDrawable, cancelPotentialWork, and getBitmapWorkerTask to a utility folder.

...so I'm calling loadBitmap and it's chain 77 times for a listview that currently has 12 rows in it (six show on the screen at load time with just a hint of the 7th showing).

So I shouldn't worry, this is okay (this number of calls to bindView & the firing off of all those subsequent methods)?

Thanks for your words.

Collings answered 7/6, 2013 at 19:19 Comment(0)
D
10

If in your listview xml android:layout_height is not "match_parent" change it to android:layout_height="match_parent"

Diamine answered 7/6, 2013 at 20:17 Comment(1)
Thank you for your words. I had the listview fragment with a height of wrap content sitting inside a Framelayout that was also wrap content, I changed both of those to match parent and the calls to bind view shot down to 7, as I originally would have wished. Thanks again.Collings
I
2

The newView method is called for each newly created view while the bindView is called once each view data is required to bind to the corresponding view. One of the reasons that cause bindView get called several times is listview recycling which recycles the views that goes out of viewport. As an example when you scroll over a listview every new view which comes to view port would cause a call to bindView. I suggest you to create a cache mechanism if your loadBitmap is resource intensive so that you shouldn't have a new call to loadBitmap for each bindView call.

Insectarium answered 7/6, 2013 at 19:37 Comment(1)
Thank you for your words. I used, per the tutorial mentioned in OP, a LruCache but that is built and maintained distal to loadBitmap; but your words have me thinking maybe there should be a cache prior to the loadBitma call; unfortunately I'm not very savvy with caches (actually the LruCache is the first one I ever implemented). Ultimately altering the XML fixed the problem but I'll keep your cache suggestion in mind. Thank you for your energy in helping me fix my woes.Collings

© 2022 - 2024 — McMap. All rights reserved.