1

I got a problem in gridview scroll. I have make one custom gridview and insert widget in raw file and inflate them through view. I got proper result and proper data and everything is gone well but when I scroll gridview 3-4 times up down speedy it raises an OutOfMemoryException.

This app contain list of installed app list and icon

Here is my custome adapter's code:

  package com.AppFavorits;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.AppFavorits.ImageLoad.ImageLoader;

public class GridViewAdapter extends BaseAdapter {
    private Context activit;
    LayoutInflater inflator;
    public RelativeLayout imgvGridItem;
    public TextView txtGridItemlabel;
    public CheckBox chkbxGridItem;
     ArrayList<PInfo> lstpinfo = new ArrayList<PInfo>();
     public ImageLoader imageLoader; 
     public GridViewAdapter(Context m1, ArrayList<PInfo> lstpinfo) {

            activit = m1;
            inflator = LayoutInflater.from(m1);
            this.lstpinfo = lstpinfo;
              imageLoader=new ImageLoader(m1.getApplicationContext());

        }

    public static class ViewHolder {

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder view;
        // LayoutInflater inflator = activit.getLayoutInflater();


        view = new ViewHolder();
        convertView = inflator.inflate(R.layout.gridviewrow, null);

        imgvGridItem = (RelativeLayout) convertView
                .findViewById(R.id.rlGreidItemicon);
        txtGridItemlabel = (TextView) convertView
                .findViewById(R.id.txtGridItemlabel);
        chkbxGridItem = (CheckBox) convertView
                .findViewWithTag(R.id.chkbxGridItem);
        if ((lstpinfo.get(position).appname.toString()) != null){
            Drawable d = lstpinfo.get(position).icon;  
            //new ImageLoad().execute();

            imgvGridItem.addView(getimageviewimage(lstpinfo.get(position).icon));
            txtGridItemlabel.setText(lstpinfo.get(position).appname.toString());

            // convertView.setTag(view);
        }

        return convertView;

    }

    @Override
    public int getCount() {
        return lstpinfo.size();
    }

    @Override
    public Object getItem(int position) {
        return lstpinfo.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public RelativeLayout getimageviewimage(Drawable d) {
        RelativeLayout seprator = new RelativeLayout(activit);
        seprator.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        seprator.setGravity(Gravity.LEFT | Gravity.CENTER);

        ImageView imgmap = new ImageView(activit);
        imgmap.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        // imgmap.setBackgroundResource(R.drawable.a13);
        Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
        imageLoader.DisplayImage(bitmap, imgmap);

        //imgmap.setImageBitmap(bitmap);
        seprator.setPadding(5, 5, 15, 5);
        seprator.addView(imgmap);

        return seprator;

    }
     private class ImageLoad extends AsyncTask<Void, Void, Void> {
           // private final ProgressDialog dialog = new ProgressDialog(tranning.this);
            protected void onPreExecute() {
            //  this.dialog.setMessage("Please Wait...");
             // this.dialog.show();
              // put your code which preload with processDialog  
            }
              @Override
              protected Void doInBackground(Void... arg0) {
                   // put your code here
                   return null;
              }

             @Override
             protected void onPostExecute(final Void unused) {
                /*if (this.dialog.isShowing()) {
                this.dialog.dismiss();
             }   */
          }
      }
}

I think there are something wrong with getview.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Nikunj Patel
  • 21,853
  • 23
  • 89
  • 133

3 Answers3

1

In getView() you are not reusing convertView, but always inflating new one with inflator. This requires more memory and makes your code slower. Also, check that you are not leaking images with imageLoader.

alders
  • 235
  • 1
  • 4
  • try this code: if (convertView == null) {convertView = inflator.inflate(R.layout.griedviewrow, null);} – alders May 29 '12 at 04:10
0

out of memory usually means that there is not enough memory to load the file. try closing everything else (or atleast a few other apps) and trying again.

Epicblood
  • 1,167
  • 2
  • 10
  • 29
0

Download code From Following link and use gridview instead of listview

Lazy Loading Example

Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128