gridview中的memoryleak使用基本适配器和viewholder方法

时间:2015-07-07 14:32:20

标签: android gridview memory-leaks baseadapter

我在gridview中使用了扩展BaseAdapter,我在其中实现了ViewHolder方法。我也用光标将数据传递给这个适配器。

这是我的getView()

public View getView(int position, View view, ViewGroup parent) {    // inflate the layout for each item of listView

        ViewHolder holder;
        if (view == null) {
            view = inflater.inflate(gridItemId, null);
            Log.d("recyvleView", "inflating " + position);

            holder = new ViewHolder();
            holder.tvAttachment = (ImageView) view.findViewById(R.id.iv_inventory_products_griditems_attachment);
            holder.imageCount = (TextView) view.findViewById(R.id.imagecount);
            holder.tvItemCode = (TextView) view.findViewById(R.id.tv_inventory_products_griditems_ItemCode);
            holder.tvProductName = (TextView) view.findViewById(R.id.tv_inventory_products_griditems_Title);
            holder.tvPrice = (RialTextView) view.findViewById(R.id.tv_inventory_products_griditems_Price);
            holder.tvRemain = (TextView) view.findViewById( R.id.tv_inventory_products_griditems_Remain);
            holder.btnMore =(com.rey.material.widget.Button) view.findViewById(R.id.btn_inventory_products_griditems_More);
            holder.btnPlus = (com.rey.material.widget.Button) view.findViewById(R.id.btn_inventory_products_griditems_addOne);

            view.setTag(holder);
        } else {
            Log.d("recyvleView", "restoring " + position);
            holder = (ViewHolder) view.getTag();
        }

        setupView(view, position,holder);


        //setupGridView(view,holder);


        return view;
    }

我注意到我有一个巨大的内存泄漏,所以我试图最小化一切以找到泄漏,所以我评论了大多数方法,这是我setupView留下的内容:

private void setupView(View view, int position, ViewHolder holder) {

        // move the cursor to required position

        cursor.moveToPosition(position);
        Log.d("POSITION", String.valueOf(position));


        //holder.itemId = cursor.getString(cursor.getColumnIndex(DatabaseColumnContent.COL_PRODUCT_ITEM_ID.toString()));
        //TSimpleProduct tempProduct = productCatalog.getSimpleProductById(Integer.parseInt(holder.itemId));
        //holder.itemGuId = cursor.getString(cursor.getColumnIndex(DatabaseColumnContent.COL_PRODUCT_GUID.toString()));
        holder.tvItemCode.setText(cursor.getString(cursor.getColumnIndex(DatabaseColumnContent.COL_PRODUCT_ITEMCODE.toString())));
        holder.tvProductName.setText(cursor.getString(cursor.getColumnIndex(DatabaseColumnContent.COL_PRODUCT_ITEMDESC.toString())));

      /*  //Remain
        if (cyberSetting.getSettingValue(TCyberSettingKey.SHOWITEMREMAIN).equals("1")) {
            textTemp = (mContext.getString(R.string.restrictedInfo));
        } else {
            if (tempProduct.getDefaultUnitValue() == 2 && tempProduct.isUnitDependent()) {
                String titleRemain2 = DatabaseColumnContent.COL_PRODUCT_CURSOR_REMAIN2.toString();
                textTemp = cursor.getString(cursor.getColumnIndex(titleRemain2));
            }
            if (cyberSetting.getSettingValue(TCyberSettingKey.SHOWITEMREMAIN).equals("2")) {
                if (textTemp == null) {
                    textTemp = "0";
                }
                int t = Integer.parseInt(textTemp);
                if (t > 0) {
                    textTemp = mContext.getString(R.string.productAvailable);
                } else {
                    textTemp = mContext.getString(R.string.productUnAvailable);
                }
            }
        }
        holder.tvRemain.setText(textTemp);

        //Price
        String priceLevel = "0";
        try {
            Register register = Register.getInstance();
            priceLevel = register.getPriceLevel();
        } catch (NoDaoSetException e) {
            e.printStackTrace();
        }
        if(!priceLevel.equals("0"))
            textTemp = cursor.getString(cursor.getColumnIndex(priceLevel));
        else
            textTemp = "0.0";

        if (tempProduct.getDefaultUnitValue() == 2 && tempProduct.isUnitDependent()) {
            double price2;
            price2 = TLineItem.convertPrice1ToPrice2(Double.parseDouble(textTemp), tempProduct.isUnit1Bigger(), tempProduct.getUnitCoef());
            textTemp = TGeneralTools.ConvertDoubleToEnglishString(price2);
            if (tempProduct.getUnitDesc2() != null && !tempProduct.getUnitDesc2().equals(""))
                unitDesc = " (" + tempProduct.getCompleteUnitDesc2() + ")";
        } else {
            if (tempProduct.getUnitDesc1() != null && !tempProduct.getUnitDesc1().equals(""))
                unitDesc = " (" + tempProduct.getCompleteUnitDesc1() + ")";
        }
        holder.priceDef = textTemp;
        holder.tvPrice.setText(textTemp + unitDesc);

        holder.tvRemain.setText(holder.tvRemain.getText() + unitDesc);


        //image
        pictureCatalog = TPictureCatalog.getInstance();
        String defGuid = "";

        if (tempProduct.getHasAttachContent() >= 1 && pictureCatalog.isDownloadedAlbumAvailable()) {
            defGuid = pictureCatalog.getDefaultPictureGuid(holder.itemGuId);
            if (tempProduct.getHasAttachContent() == 1) {
                holder.imageCount.setVisibility(View.GONE);
            } else {
                holder.imageCount.setVisibility(View.VISIBLE);
                holder.imageCount.setText(String.valueOf(tempProduct.getHasAttachContent()));
            }
        } else {
            holder.imageCount.setVisibility(View.GONE);
        }

        String filename = Environment.getExternalStorageDirectory().getPath()
                + FileAddressContent.APPLICATION_HOME_DIRECTORY
                + FileAddressContent.PICTURES_ROOT_DIRECTORY
                //+ FileAddressContent.PICTURES_THUMBS_DIRECTORY.toString()
                + defGuid + FileAddressContent.PICTURES_EXTENSION;
        pic = new File(filename);
        if (pic.exists())
            Picasso.with(mContext)
                    .load(pic)
                    .error(R.drawable.noimage)
                            //.placeholder(R.drawable.loading)
                    .resize(thumbSize, thumbSize)
                    .centerInside()
                    .into(holder.tvAttachment);
        else
            Picasso.with(mContext)
                    .load(R.drawable.noimage)
                    .resize(thumbSize, thumbSize)
                    .centerInside()
                    .into(holder.tvAttachment);
        holder.tvAttachment.setMinimumHeight(thumbSize);


        view.setTag(holder);*/

    }

即使几乎所有的都被评论,这两行仍在泄漏,我知道当我评论它们时,没有泄漏......!

 holder.tvItemCode.setText(cursor.getString(cursor.getColumnIndex(DatabaseColumnContent.COL_PRODUCT_ITEMCODE.toString())));
        holder.tvProductName.setText(cursor.getString(cursor.getColumnIndex(DatabaseColumnContent.COL_PRODUCT_ITEMDESC.toString())));

这也是我的持有人类:

public class ViewHolder {
    public ImageView tvAttachment;
    public ImageView ivStatus;
    public TextView imageCount;
    public TextView tvItemCode;
    public TextView tvProductName;
    public TextView tvRemain;
    public RialTextView tvPrice;

    public String priceDef;
    public String itemId;
    public String itemGuId;
    public Button btnMore;
    public Button btnPlus;

}

有什么建议吗?怎么能阻止它泄漏?! 提前谢谢!

编辑: 我也使用游标适配器获得相同的结果。

1 个答案:

答案 0 :(得分:0)

您为什么使用BaseAdapter? Android API有CursorAdapterCursorLoader,用于处理所有游标内容。看起来你正在做游标错误,这会导致内存泄漏。