Hello, I'm doing an application for my tcc, at the moment it is looking for a list of images in the database and showing in a listview in android, but when I make the list transition, it does search again, this does with the application getting too heavy, so I'd like to cache the bitmap, so when I open the app it uses the images I downloaded. I'm getting a byte array and transforming into a bitmap, this inside a baseAdapter and I'd like to know how I can cache it.
public class UsuarioAdapter extends BaseAdapter {
private List<Usuario> usrs;
private Context context;
private LayoutInflater inflater;
public UsuarioAdapter(Context context, List<Usuario> usrs,
ImageLoader mImageLoader) {
this.usrs = usrs;
this.context = context;
this.inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mImageLoader = mImageLoader;
}
@Override
public int getCount() {
return usrs.size();
}
@Override
public Object getItem(int arg0) {
return usrs.get(arg0);
}
@Override
public long getItemId(int arg0) {
return usrs.get(arg0).getId();
}
@Override
public View getView(int position, View view, ViewGroup parent) {
ViewHolder holder = null;
if (view == null) {
holder = new ViewHolder();
int layout = R.layout.lista_de_usuarios;
view = inflater.inflate(layout, null);
view.setTag(holder);
holder.ivFoto = (ImageView) view.findViewById(R.id.ivLvFoto);
holder.tvNome = (TextView) view.findViewById(R.id.tvLvNome);
holder.tvIdade = (TextView) view.findViewById(R.id.tvLvIdade);
} else {
holder = (ViewHolder) view.getTag();
}
Usuario usr = usrs.get(position);
holder.tvNome.setText(usr.getNome());
holder.tvIdade.setText(usr.getIdade() + " Anos");
Bitmap bitmap = BitmapFactory.decodeByteArray(usr.getFoto(), 0,
usr.getFoto().length);
holder.ivFoto.setImageBitmap(bitmap);
return view;
}
static class ViewHolder {
ImageView ivFoto;
TextView tvNome;
TextView tvIdade;
}
}
The bitmap I'm referring to is in this line of code:
Bitmap bitmap = BitmapFactory.decodeByteArray(usr.getFoto(), 0,
usr.getFoto().length);
holder.ivFoto.setImageBitmap(bitmap);