开发者

Content of ListView takes to much memory

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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜