0

I am populating GridView with multiple item,each containing ImageView and TextView, there will be about 100+ such items. Problem is everything is executing properly but activity is lagging heavily while scrolling, I tried to load bitmap efficiently by limiting imagesize but with no improvement.

And also note my source of images is Local Filesystem and Category.getCategoryUrl() contains filepath.

Here is my ArrayAdapter implementation:

public class RootCategoryAdapter extends ArrayAdapter<Category> {

    private int resource;
    private Context context;
    private ArrayList<Category> categoryList;
    private DisplayMetrics metrics;

    public RootCategoryAdapter(Context context, int resource,
            ArrayList<Category> categoryList, DisplayMetrics metrics) {
        super(context, resource, categoryList);
        // TODO Auto-generated constructor stub
        this.context = context;
        this.resource = resource;
        this.categoryList = categoryList;
        this.metrics = metrics;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        CategoryHolder holder = null;

        if (convertView == null) {
            // LayoutInflater inflater =(LayoutInflater)
            // context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            convertView = inflater.inflate(resource, parent, false);

            holder = new CategoryHolder();
            holder.title = (TextView) convertView
                    .findViewById(R.id.root_category_text);
            holder.image = (ImageView) convertView
                    .findViewById(R.id.root_category_image);

            convertView.setTag(holder);
        } else {
            holder = (CategoryHolder) convertView.getTag();
        }

        Category category = categoryList.get(position);

        holder.title.setText(category.getCategoryName());

        // I think problem starts here but not certain
        holder.options = new BitmapFactory.Options();
        holder.options.inJustDecodeBounds = true;
        holder.bitmap = BitmapFactory.decodeFile(category.getCategoryUrl(),
                holder.options);
        holder.options.inJustDecodeBounds = false;
        holder.options.inSampleSize = BasicChores.calculateInSampleSize(
                holder.options, 200, 200);
        holder.bitmap = BitmapFactory.decodeFile(category.getCategoryUrl(),
                holder.options);

        holder.image.setImageBitmap(holder.bitmap);

        return convertView;

    }

    static class CategoryHolder {
        Bitmap bitmap;
        BitmapFactory.Options options;
        TextView title;
        ImageView image;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return categoryList.get(position).getCategoryId();
    }

    @Override
    public Category getItem(int position) {
        // TODO Auto-generated method stub
        return categoryList.get(position);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return categoryList.size();
    }

    @Override
    public int getPosition(Category item) {
        // TODO Auto-generated method stub
        return categoryList.indexOf(item);
    }

}
Hardip Patel
  • 85
  • 2
  • 11
  • load you bitmaps in the background. google "lazy image loading" – Sherif elKhatib Oct 27 '13 at 00:21
  • @SherifelKhatib "lazy image loading" is not getting me anywhere can you if possible describe me more precisely..sorry – Hardip Patel Oct 27 '13 at 00:27
  • check all the answers in this (I would advice you to use Nostra's Universal Image Loader" http://stackoverflow.com/questions/541966/how-do-i-do-a-lazy-load-of-images-in-listview – Sherif elKhatib Oct 27 '13 at 00:30
  • ya but I already have images in my FileStorage so I would appreciate workaround rather then creating UI Thread...I saw GreenDroid's one but it just downloads Images – Hardip Patel Oct 27 '13 at 00:36
  • [`String imageUri = "file:///mnt/sdcard/image.png"; // from SD card`](https://github.com/nostra13/Android-Universal-Image-Loader) – zapl Oct 27 '13 at 01:16
  • @zapl I tried using universal Image Loader but it quite did not work well, Application class is misbehaving I guess... – Hardip Patel Oct 28 '13 at 07:08

1 Answers1

1

You should probably caching your images

Check this out:

http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

Björn Hallström
  • 3,775
  • 9
  • 39
  • 50