0

I'n my application I have a ListView which has 2 TextViews and 2 ImageViews, I've noticed that when I scroll it, it's not completely smooth, I've searched around and noticed that I need a background worker thread that needs to render my ImageViews in my listView. I tried to implement it but I couldn't because I was making references to the viewHolder class from inside my Thread, so the holder needed to be final, but that couldn't happen, so probably I was doing it wrong.Does anybody have any idea on how this can be achieved? Thank you in advance

   @Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    MonumentHolder holder = null;

    if(row == null){

        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(resource, parent, false);

        holder = new MonumentHolder();
        holder.imgIcon = (ImageView)row.findViewById(R.id.imageView3);
        holder.txtTitle = (TextView)row.findViewById(R.id.textView1);
        holder.marker =  (ImageView)row.findViewById(R.id.imageView2);
        holder.descr = (TextView)row.findViewById(R.id.textView2);

        row.setTag(holder);

    }
    else{

        holder = (MonumentHolder)row.getTag();

    }

    Monument monument = monuments.get(position);
    holder.txtTitle.setText(monument.getTitle());

    if(monument.getTitleOfCategory().equals("Βυζαντινά Μνημεία"))
        holder.marker.setImageResource(R.drawable.memorial);
    else if(monument.getTitleOfCategory().equals("Αρχαιολογικά Μνημεία"))
        holder.marker.setImageResource(R.drawable.ar);
    else if(monument.getTitleOfCategory().equals("Ιστορικά")) 
        holder.marker.setImageResource(R.drawable.hist);
    else
        holder.marker.setImageResource(R.drawable.museum);


    localPhotosUrl=monument.getLocalPhotosUrl();

    holder.imgIcon.setImageBitmap(null);

    if(!localPhotosUrl.isEmpty()){

        File picToLoad = new File(localPhotosUrl.get(0));
        byte[] lala = null;
        try {
            lala = org.apache.commons.io.FileUtils.readFileToByteArray(picToLoad);
            s(lala.length);
            System.gc();
            Bitmap bitmap = BitmapFactory.decodeByteArray(lala, 0, lala.length);

            holder.imgIcon.setImageBitmap(bitmap);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

    holder.descr.setText(monument.getContent());


    return row; 
}
Libathos
  • 3,340
  • 5
  • 36
  • 61

2 Answers2

1

I suggest you to use Picasso for image loading/ handling cache and rendering on an imageview. Once you have the jar in your workspace, you just need one line of code.

Instead of :

holder.imgIcon.setImageBitmap(null);

if(!localPhotosUrl.isEmpty()){

    File picToLoad = new File(localPhotosUrl.get(0));
    byte[] lala = null;
    try {
        lala = org.apache.commons.io.FileUtils.readFileToByteArray(picToLoad);
        s(lala.length);
        System.gc();
        Bitmap bitmap = BitmapFactory.decodeByteArray(lala, 0, lala.length);

        holder.imgIcon.setImageBitmap(bitmap);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}

you can replace it with

Picasso.with(context).load(localPhotosUrl).into(holder.imgIcon);
droidx
  • 2,172
  • 19
  • 26
0

What you are looking for is called 'Lazy Loading' for the images. Loading the images in the GetView slows it down. The image loading has be done in a thread.

See: Lazy load of images in ListView

Community
  • 1
  • 1
Sugarel
  • 811
  • 1
  • 13
  • 22