Why are you putting the Bitmap inside the ViewHolder? That means you are always loading the bitmaps, which is very slow. You could simply use a list of SoftReference<Bitmap> to store them.
On Wed, May 6, 2009 at 1:11 PM, sam <[email protected]> wrote: > > Hey guys, I am writing an app that has an activity that is a > ListActivity which implements its own listadapter. Each row is > defined by an xml layout, and the contents of each row is an ImageView > (initially bytes stored in our own database and reconstructed into an > image on getView()) and a TextView (whose text is contact name from > the contact provider database). We are also allowing the user to > launch other activities from our ListActivity by long clicking on a > row and selecting from the context menu "view contact", which will > launch Intent.ACTION_VIEW, which is the activity that displays all of > the information about the relevant contact. > > So it seems like we have a problem that gets compounded every time a > call to getView() is made. Basically, when we first start up the list > view, we can start the intent to view a gestures information and > subsequently hit the back button to finish the viewing activity and > return back to our list activity without any real problems. But, the > more you scroll through the list, and the more you view contact > information and come back to the list (inducing more calls to getView > ()), the longer the phone takes to return back to our ListActivity, > eventually prompting the "not responding" screen. The problem almost > certainly has no relevance to the activity that is launched from our > list activity because we have tried opening up random apps rather than > Intent.ACTION_VIEW and returning to our list view and it resulted in > the same behavior. What does seem to be relevant is having the list > activity lose focus and then try to regain it. I have suspicions that > we are leaking drawables when calling getView and trying to recycle, > but it doesn't make all that much sense because I have spent minutes > just scrolling the list with no memory issues. We have tried testing > how much time it took for our program to execute the relevant > functions that are a part of every application's life-cycle (onPause, > onResume, onStop, onRestart, onStart), to see if there is any > significant slow down in any of those calls, but they are all very > fast. The only correlation we have witnessed is the more calls to > getView(), the worse the time gets between finishing the activity on > top of our list activity and putting our list activity back into > focus. We have been stumped for the past few days, our only recourse > up until now has been to try to explicitly null the references to > everything that gets recycled to encourage garbage collection, but > quite honestly I am out of ideas for why this could be happening. > Here is our code for getView(): > > @Override > public View getView(int position, View convertView, > ViewGroup > parent) { > > ViewHolder holder; > if (convertView == null) { > convertView = > mInflater.inflate(R.layout.list_view, null); /*our > view holding a tview and iview*/ > > > // Creates a ViewHolder and store > references to the two children > views > // we want to bind data to. > holder = new ViewHolder(); > holder.guts = (TextView) > convertView.findViewById(R.id.text); > holder.icon = (ImageView) > convertView.findViewById(R.id.icon); > holder.icon.setImageBitmap(null); > // init this stuff > holder.bitmapD = null; > holder.bitmap = null; > > convertView.setTag(holder); > } else { > // Get the ViewHolder back to get fast > access to the TextView > // and the ImageView. > holder = (ViewHolder) > convertView.getTag(); > holder.icon.setImageBitmap(null); > } > > // null the old stuff; > > holder.bitmapD = null; > > if(holder.bitmap != null) { > holder.bitmap.recycle(); > holder.bitmap = null; > } > > Cursor c1 = (Cursor) getItem(position); > > ContactsListActivity.this.startManagingCursor(c1); > String name = > c1.getString(c1.getColumnIndexOrThrow(People.NAME)); > > long personID = > c1.getLong(c1.getColumnIndexOrThrow(People._ID)); > Uri temp = Uri.withAppendedPath > (ContactGesturesProvider.GESTURES_CONTENT_URI, "contacts"); > final Uri personURI = > Uri.withAppendedPath(temp, "" + personID); > Cursor c = mContext.managedQuery(personURI, > null, null, null, > null); > > BitmapDrawable bd = null;// = new > BitmapDrawable(); > if(c.moveToFirst()) { > byte[] work = > c.getBlob(c.getColumnIndexOrThrow > (ImageProvider.IMAGE_COLUMN)); > Bitmap newb = > BitmapFactory.decodeByteArray(work, 0, > work.length); > bd = new BitmapDrawable(newb); > > holder.bitmap = newb; > } > c.close(); > // Bind the data efficiently with the holder. > holder.guts.setText(name); > holder.icon.setImageDrawable(bd); > holder.bitmapD = bd; > > return convertView; > } > > and our holder: > > // stuff for views > private static class ViewHolder { > ImageView icon; > TextView guts; > BitmapDrawable bitmapD; > Bitmap bitmap; > } > > Thanks ahead of time for anyone who has made it this far, let alone > for posting a reply! > > -Sam > > > -- Romain Guy Android framework engineer [email protected] Note: please don't send private questions to me, as I don't have time to provide private support. All such questions should be posted on public forums, where I and others can see and answer them --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---

