I'm trying to make a custom listview adaptar that can update the imageView in each view asynchronously so that it doesn't slow down.
So far I'm using a vector that storage the bitmaps of the imageviews and in the adapter constructor I'm calling a thread that gets all the bitmaps. In the getView() method I check if the vector[position] is not null to actually change the imageView bitmap.
here is the code:
public class SongAdapter extends ArrayAdapter<Song> {
private int resource;
private LayoutInflater inflater;
private ArrayList<Song> items;
private Context cntx;
private final Utilities util = new Utilities(); //una per tutte le canzoni
private Bitmap[] covers; //la utilizzo per fare un cashing delle cover
public SongAdapter(Context cntx, int resId, ArrayList<Song> objects)
{
super(cntx, resId, objects);
this.cntx = cntx;
items = objects;
resource = resId;
inflater = LayoutInflater.from(cntx);
covers = new Bitmap[objects.size()];
asyncLoadImagesCover(); //lacio un thread per caricare le immagini
}
//carico le immagini nella listview in background
public void asyncLoadImagesCover()
{
new Thread(new Runnable() {
@Override
public void run() {
for(int i = 0; i < items.size(); i++)
covers[i] = util.getAlbumArtFromSong(items.get(i).getPath(), cntx);
//notifyDataSetChanged();
}
}).start();
}
//cerco di mantenere in memoria l'oggetto in modo da evitare il ricaricamento
private static class ViewHolder
{
TextView titolo;
TextView artista;
ImageView cover;
}
//////////////////////////////////////////////////////////////////////////////////////
//Funzione che riscrive gli elementi della listview
@Override
public View getView(int position, View v, ViewGroup parent)
{
//recupero l'oggetto canzone
Song song = getItem(position);
ViewHolder holder;
if(v == null)
{
//creo la view partendo dal layout
v = inflater.inflate(resource, parent, false);
holder = new ViewHolder();
holder.titolo = (TextView)v.findViewById(R.id.songTitle);
holder.artista = (TextView)v.findViewById(R.id.artistText);
holder.cover = (ImageView)v.findViewById(R.id.albumImage);
v.setTag(holder);
}
else
holder = (ViewHolder) v.getTag();
//setto i valori a partire dall'holder, in questo modo non devo fare findviewbyid ogni volta
holder.titolo.setText(song.getTitle());
holder.artista.setText(song.getArtist());
if(covers[position] != null)
holder.cover.setImageBitmap(covers[position]);
return v;
}
}
Anyway this isn't working really well, the 50% of the times the activity crashes and I guess it's because of the new thread. I searched on the web and found different solutions, but I wanna first understand why my code is crushing. Also, how can I make a thread that updates my listview gui passing v and position in getView?
////////////////////////////////////////////// EDIT:
For anyone interested, this is what I've done: I've made a class with two separate object in it that uses one handler to update the UI and an ExecutorService to stack threads that get the image by my function:
public class CoverLoader {
Utilities util = new Utilities();
Context cntx;
ExecutorService executorService;
Handler handler = new Handler(); //si occupa di aggiornare la UI
public CoverLoader(Context cntx)
{
this.cntx = cntx;
executorService = Executors.newFixedThreadPool(5);
}
public void DisplayImage(ImageView iv, String path)
{
executorService.submit(new ImageLoader(iv,path));
}
//Separo la parte del caricamento dalla parte di aggiornamento della gui
//questa classe si occupa solo di recuperare la bmp
public class ImageLoader implements Runnable
{
ImageView iv;
String path;
public ImageLoader(ImageView iv, String path)
{
this.iv = iv;
this.path = path;
}
@Override
public void run()
{
Bitmap bmp = util.getAlbumArtFromSong(path, iv.getContext());
handler.post(new BitmapDisplayer(iv,bmp));
}
}
//Si occupa di aggiornare la UI, richiamata dall'handler a termine del processo di caricamento
class BitmapDisplayer implements Runnable
{
Bitmap bitmap;
ImageView iv;
public BitmapDisplayer(ImageView iv, Bitmap bitmap)
{
this.iv = iv;
this.bitmap = bitmap;
}
@Override
public void run()
{
iv.setImageBitmap(bitmap);
}
}
}
Now on the getView():
@Override
public View getView(int position, View v, ViewGroup parent)
{
Song song = getItem(position);
ViewHolder holder;
if(v == null)
{
//creo la view partendo dal layout
v = inflater.inflate(resource, parent, false);
holder = new ViewHolder();
holder.titolo = (TextView)v.findViewById(R.id.songTitle);
holder.artista = (TextView)v.findViewById(R.id.artistText);
holder.cover = (ImageView)v.findViewById(R.id.albumImage);
v.setTag(holder);
}
else
holder = (ViewHolder) v.getTag();
//setto i valori a partire dall'holder, in questo modo non devo fare findviewbyid ogni volta
holder.titolo.setText(song.getTitle());
holder.artista.setText(song.getArtist());
coverLoader.DisplayImage(holder.cover, song.getPath()); //Richiamo uno stack di thread
//if(covers[position] != null)
//holder.cover.setImageBitmap(covers[position]);
return v;
}
So far so good, now I will just find a way (an array maybe?) to save the image that I've already got