I haven't read through the whole thread, but FWIW there's a very good introduction to list view 'lazy loading' techniques in Beginning Android: http://www.apress.com/book/view/1430224193.
Look in the sample code under fancylists -- well documented in the book. Sam Dutton On Dec 8 2009, 2:34 pm, Samuh <samuh.va...@gmail.com> wrote: > I trying a hand at ListViews and my current experiment is aimed at > displaying some data in a ListView. The data to be displayed in each > row is simple: an image and some text. The images come from a remote > server and the textual data is hardcoded. > > I have a class that downloads images using AsyncTask and caches the > list of images fetched as SoftReferences in a LinkedHashMap. I am also > passing a reference of the view to this class, so when the image > download/cache read is complete the class will set appropriate Bitmap > in the view. > > Following is my code: > > //class BasicAdapter extends ArrayAdapter<RowData> > ... > static class ViewHolder{ > TextView text; > ImageView icon; > } > > @Override > public View getView(int position, View convertView, ViewGroup parent) > { > ViewHolder holder; > if(convertView == null){ > convertView = > mInflater.inflate(R.layout.listview_row,null); > > holder = new ViewHolder(); > holder.text = (TextView) > convertView.findViewById(R.id.row_txt); > holder.icon = (ImageView) > convertView.findViewById(R.id.row_img); > > convertView.setTag(holder); > } > else{ > holder = (ViewHolder) convertView.getTag(); > } > > holder.text.setText(mDataSet.get(position).getMText()); > > //set default icon > holder.icon.setImageResource(R.drawable.icon); > > // set the actual image from the cache > String imageUrl = mDataSet.get(position).getMUrl(); > WriteThroughCache.getImage(holder.icon, imageUrl); > > return convertView; > > } > > // My helper class that manages cache and image Download > > public class WriteThroughCache{ > static LinkedHashMap<String,SoftReference<Bitmap>> imageCache = > new LinkedHashMap<String,SoftReference<Bitmap>>(); > > public static void getImage(ImageView icon, String imageUrl){ > if(imageCache.containsKey(imageUrl)){ > Bitmap image = imageCache.get(imageUrl).get(); > if(image != null){ > icon.setImageBitmap(image); > return; > } > } > > new LoadImageInBackground().execute(new > Object[]{icon,imageUrl}); > > } > > static class LoadImageInBackground extends AsyncTask<Object, Void, > Bitmap>{ > ImageView mIcon = null; > String mUrl = null; > @Override > protected Bitmap doInBackground(Object... params) { > mIcon = (ImageView)params[0]; > mUrl = (String) params[1]; > Bitmap image = BitmapFactory.decodeStream > (GetMethodExecutor.getResponseStream(mUrl)); > return image; > } > @Override > protected void onPostExecute(Bitmap result) { > super.onPostExecute(result); > mIcon.setImageBitmap(result); > imageCache.put(mUrl, new > SoftReference<Bitmap>(result)); > } > > } > > } > > Problem: The applications seems to work just fine. However, when I > touch scroll the screen sometimes, the rows display the default image > and "Loading.." text. These are never refreshed. > But if I fling the listview, everything works normal then. > > I am not sure of what I am missing here, please guide. > > Thanks.
-- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en