lundi 13 avril 2015

Android ListView image flickers while trying to load the image from SQLite Database

I implemented ListView to display some items from the SQLite Database. Each of which contains an Image and some Data.


I want my ListView to work faster even if it contains thousand rows of data. So i tried to implement some optimizations that i have noticed. Here is the basic structure of my CustomCursorAdapter:



Class CustomCursorAdapter extends CursorAdapter
{
Cursor cursor;
public CustomCursorAdapter(..., Cursor _cursor)
{
cursor = _cursor;
}

public void bindView(View _view, Context _context, Cursor _cursor)
{
if( view.getTage() == null)
{
//create and initialize a new holder and set it to view tag.
holder = new Holder();
...
...
holder.imageView = (ImageView) view.findViewById(R.id.image);
holder.imageView.setImageDrawable(defaultDrawable);

view.setTag(holder);
}

String mediaID = _cursor.getString("media_id");

//create an asyncTask to load the image from database
new MediaLoader(context, holder.imageView).execute(mediaID);
}

private class MediaLoader extends AsyncTask<String, Void, Media>
{
private Context context;
private final WeakReference <ImageView> imageViewReference;

public MediaLoader(Context _context, ImageView _imageView)
{
context = _context;
imageViewReference = new WeakReference<ImageView>(_imageView);
}

protected Media doInBackground(String... args)
{
String _mediaID = args[0];
return MediaDataManager.getInstance(_context).getMediaObjectForListAdapter(_mediaID);
}

protected void onPostExecute(final Media _media)
{
super.onPostExecute(_media);

if( imageViewReference != null )
{
ImageView _imageView = imageViewReference.get();

if(_imageView != null)
{
if( _media != null && _media.getImage() != null )
{
_imageView.setImageBitmap(_media.getImage());
}
else
{
_imageView.setImageDrawable(defaultDrawable);
}
}
}
}
}//Media Loader ends.
}//Custom Cursor Adapter Ends.


Using this approach loading time of image seemed ok to me.


But in some android devices (low configuration ones), i am experiencing image flickering. For some reason during scrolling or even loading i noticed images keeps changing throughout the list. But the final image that remains in every row is always the correct one.


I couldn't find any helpful resource by searching. Any kind of help is very much appreciated.


Aucun commentaire:

Enregistrer un commentaire