1

The ListView contains rather many items(20 or more), and each of them is an ImageView with a bitmap source. These bitmaps are not compressed and have size about 640x480. When the ListView is just loaded, it takes a few memory, but if I'll scroll it then the error "Memory exceed VM budjet" may happen. When I replace the fragment with another fragment, the ListView is still hanging in memory. How can I clear the ram/ListView cache?

Update :

I use a SimpleCursorAdapter with a ViewBinder:

    SimpleCursorAdapter.ViewBinder viewBinder = new SimpleCursorAdapter.ViewBinder() {
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            if (columnIndex == 0) {
                TextView tv1 = (TextView) view;
                if (cursor.getInt(4) == 0)
                    tv1.setTextColor(Color.GRAY);
                else
                    tv1.setTextColor(Color.parseColor("#191919"));
                tv1.setText(cursor.getString(columnIndex));
            }
            if (columnIndex == 3) {
                TextView tv1 = (TextView) view;
                tv1.setText(cursor.getString(columnIndex));
            }
            if (columnIndex == 2) {
                final ImageView image = (ImageView) view;
                Bitmap cachedImage = null;
                try {
                    cachedImage = imageThreadLoader.loadImage(cursor.getString(columnIndex), new ImageThreadLoader.ImageLoadedListener() {
                        public void imageLoaded(Bitmap imageBitmap) {
                            image.setImageBitmap((Bitmap) new SoftReference(imageBitmap).get());
                        }
                    });
                } catch (MalformedURLException mue) {
                    Log.e("Feeds fragment", "Can't load image");
                }
                if (cachedImage != null) {
                    image.setImageBitmap(cachedImage);
                } else image.setImageDrawable(getResources().getDrawable(R.drawable.icon));
            }

Images load in a separate thread.

user
  • 86,916
  • 18
  • 197
  • 190
RChugunov
  • 794
  • 2
  • 11
  • 26

2 Answers2

3

I recommend you to watch the Google IO presentation about ListViews. It contains memory saving techniques and it helped me to understand the logics of the ListViews.

naturlecso
  • 196
  • 1
  • 7
2

build the listview like this make your custom adapter and set it to the listview

public class InteractiveArrayAdapter extends ArrayAdapter<Model> {

    private final List<Model> list;
    private final Activity context;

    public InteractiveArrayAdapter(Activity context, List<Model> list) {
        super(context, R.layout.main, list);
        this.context = context;
        this.list = list;
    }

    static class ViewHolder {
        protected TextView text;
        protected CheckBox checkbox;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.main, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.text = (TextView) view.findViewById(R.id.listitem_text);
            viewHolder.checkbox = (CheckBox) view.findViewById(R.id.checkBox1);
            viewHolder.checkbox
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        public void onCheckedChanged(CompoundButton buttonView,
                                boolean isChecked) {
                            Model element = (Model) viewHolder.checkbox
                                    .getTag();
                            element.setSelected(buttonView.isChecked());

                        }
                    });
            view.setTag(viewHolder);
            viewHolder.checkbox.setTag(list.get(position));
        } else {
            view = convertView;
            ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.text.setText(list.get(position).getName());
        holder.checkbox.setChecked(list.get(position).isSelected());
        return view;
    }
}

try this way,and your unnecessary view will be recycled and the excecution will be faster and hope u will get rid of the error

MKJParekh
  • 34,073
  • 11
  • 87
  • 98
  • If I use your, I'll have to put bitmap's list in adapter's constructor, and it'll be storing in ram for all the lifeperiod of fragment, but I want to load the image in memory from external storage when I see the necessary list item, and unload bitmap when it disappear(when I scroll list) – RChugunov Sep 01 '11 at 08:22
  • sorry i havent done anything like that till yet..but i will give it a try..and let you know if i found any way.. – MKJParekh Sep 01 '11 at 08:28