1

我在 gridview 滚动中遇到问题。我制作了一个自定义网格视图并在原始文件中插入小部件并通过视图膨胀它们。我得到了正确的结果和正确的数据,一切都很顺利,但是当我快速向下滚动 gridview 3-4 次时,它会引发OutOfMemoryException.

此应用程序包含已安装应用程序列表和图标列表

这是我的客户适配器的代码:

  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();
             }   */
          }
      }
}

我认为有什么问题getview

4

3 回答 3

1

在 getView() 中,您不是重用 convertView,而是总是用充气机充气新的。这需要更多内存并使您的代码变慢。另外,请检查您是否没有使用 imageLoader 泄漏图像。

于 2012-05-29T04:05:48.053 回答
0

从以下链接下载代码并使用gridview而不是listview

延迟加载示例

于 2012-05-29T05:18:41.297 回答
0

out of memory通常意味着没有足够的内存来加载文件。尝试关闭其他所有应用程序(或至少几个其他应用程序)并重试。

于 2012-05-29T03:46:35.033 回答