CursorAdapter downloading images problems.

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

CursorAdapter downloading images problems.

Postby mini_dez » Tue Jun 02, 2009 1:00 pm

Hi,

I'm having trouble with my application which has a listview which is populated from a database. One of the columns in the database is a URL of an image which is downloaded and shown as a thumbnail in the list.

I've extended extended SimpleCursorAdapter to populate my list. I start a new thread to download the image, using AsyncTask, and set the appropriate ImageView with the downloaded image when it's done.

The problem I'm having is when my list is shown the images "flicker" between a couple of the images that are being downloaded e.g. list item one shows the thumbnail for list item 2, then 3 before it settles on the correct image. This also happens when I scroll the list. The images do finally settle on the correct list items, I'd just prefer it to do it straight away.

Here is the code I'm using for MySimpleCursorAdapter. If it would help to see any other code then let me know and I'll share.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class MySimpleCursorAdapter extends SimpleCursorAdapter {
  2.  
  3.     private Cursor _cursor;
  4.  
  5.     private Context _context;
  6.  
  7.  
  8.  
  9.     public MySimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
  10.  
  11.         super(context, layout, c, from, to);
  12.  
  13.         _cursor = c;
  14.  
  15.         _context = context;
  16.  
  17.     }
  18.  
  19.  
  20.  
  21.     public void bindView(View view, Context context, Cursor cursor) {
  22.  
  23.         ImageView imageView = (ImageView) view.findViewById(R.id.search_result_image);
  24.  
  25.        
  26.  
  27.         int id = _cursor.getColumnIndex("image");
  28.  
  29.                
  30.  
  31.         URL url=null;
  32.  
  33.                 try {
  34.  
  35.                         url = new URL(_cursor.getString(id));
  36.  
  37.                 } catch (MalformedURLException e) {
  38.  
  39.                         e.printStackTrace();
  40.  
  41.                 }
  42.  
  43.                
  44.  
  45.         try {
  46.  
  47.                         // start task to download image
  48.  
  49.                 new ImageDownloadTask().execute(new Object[] {imageView, url});
  50.  
  51.         } catch(RejectedExecutionException ignored) {}
  52.  
  53.        
  54.  
  55.         super.bindView(view, context, cursor);
  56.  
  57.     }
  58.  
  59.    
  60.  
  61.     private class ImageDownloadTask extends AsyncTask {
  62.  
  63.        
  64.  
  65.         private ImageView iv;
  66.  
  67.        
  68.  
  69.                 protected Object doInBackground(Object... params) {
  70.  
  71.                         iv = (ImageView) params[0];
  72.  
  73.                         Bitmap bm = null;
  74.  
  75.                        
  76.  
  77.                         InputStream is=null;
  78.  
  79.                         try {
  80.  
  81.                                 Log.d(TAG, "Downloading Image");
  82.  
  83.                                 is = ((URL)params[1]).openConnection().getInputStream();
  84.  
  85.                         } catch (MalformedURLException e) {
  86.  
  87.                                 e.printStackTrace();
  88.  
  89.                         } catch (IOException e) {
  90.  
  91.                                 e.printStackTrace();
  92.  
  93.                         }
  94.  
  95.                         bm = BitmapFactory.decodeStream(is);
  96.  
  97.                         return bm;
  98.  
  99.                 }
  100.  
  101.        
  102.  
  103.                 protected void onPostExecute(Object result) {
  104.  
  105.                         // set image
  106.  
  107.                         iv.setImageBitmap((Bitmap) result);
  108.  
  109.                 }
  110.  
  111.     }
  112.  
  113. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


I know I must be mixing up synchronisation between ImageViews with list positions somehow but I can't see where it's going wrong. Help much appreciated.
mini_dez
Freshman
Freshman
 
Posts: 2
Joined: Thu Feb 05, 2009 2:34 pm

Top

Postby schrotti08 » Mon Aug 31, 2009 8:57 pm

Have you ever figured out how to make this work? I have exactly the same problem and just cant seem to find the right solution. would be interesting to see the results - if you have got it working and are willing to share :) .
schrotti08
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Jul 13, 2009 6:34 pm

Postby mini_dez » Thu Sep 03, 2009 12:45 pm

No, I never found a solution. I've moved on to other parts of my project and I'll go back to it one day with fresh eyes.
If I figure it out then I'll post it here.
mini_dez
Freshman
Freshman
 
Posts: 2
Joined: Thu Feb 05, 2009 2:34 pm

Postby Legend » Sun Oct 04, 2009 11:07 pm

I know its been a long time, but did you manage a solution for this? Why not do something like this?

Replace
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. try {
  3.  
  4.                // start task to download image
  5.  
  6.           new ImageDownloadTask().execute(new Object[] {imageView, url});
  7.  
  8.         } catch(RejectedExecutionException ignored) {}
  9.  
  10.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


with
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. try {
  3.  
  4.           imageView.setImageResource(R.drawable.imageloadingplaceholder);
  5.  
  6.                // start task to download image
  7.  
  8.           new ImageDownloadTask().execute(new Object[] {imageView, url});
  9.  
  10.         } catch(RejectedExecutionException ignored) {}
  11.  
  12.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


At least, if we do this, it does not flicker the images... The imageloadingplaceholder can be a proper placeholder or just an image that blends into the background. What do you think?
Legend
Junior Developer
Junior Developer
 
Posts: 12
Joined: Tue Sep 29, 2009 1:04 am

CustomAdapter downloading images problem.

Postby pmskiran » Tue Oct 13, 2009 1:18 pm

Hi,

I am also facing the same problem if you found any way for this please reply....

Actually the first row image only showing the all rows images while downloading (one by one), after completion of downloading except first and last row remaining rows images are shown properly.. the first row image showing the last row image and last row image is not showing anything(means it is showing default image).

On observing the threads created for downloading the images, after completion of downloading also one thread is alive... i think because of that alive thread i am not able to quit the application properly...how to stop the alive thread...?

and any solutions...for this scenario..
Kiran PMS
pmskiran
Junior Developer
Junior Developer
 
Posts: 12
Joined: Fri Jul 31, 2009 10:44 am
Location: India

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 7 guests