0

我有一个像列表视图一样的网格视图。它在第一次运行时正常工作,但是当按下并返回具有 gridView 的活动时,会出现一些错误......

logCat:android.database.CursorWindowAllocationException:2048 kb 的光标窗口分配失败。# Open Cursors=761 (# 此proc打开的游标=761)

我看这样的问题,解决方案总是关于光标。我关闭光标,填充项目信息时..但它没有工作......

我的网格视图代码:

private void refreshList(String sql)
{
    gridArray = new ArrayList<Stock>();
    final Cursor cursor = _SQLite.RawQueryTry(sql, null);
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2;
    if (cursor != null)
    {
        if (cursor.moveToFirst())
        {
            for (int i = 0; i < cursor.getCount(); i++)
            {
                String stockName = cursor.getString(cursor.getColumnIndex("STOK_ADI"));
                String stockNo = cursor.getString(cursor.getColumnIndex("STOK_NO"));
                String stockCode = cursor.getString(cursor.getColumnIndex("STOK_KODU"));
                String stockEntity = cursor.getString(cursor.getColumnIndex("BIRIM"));
                String stockKdvOranı = cursor.getString(cursor.getColumnIndex("KDV_ORANI"));
                String stockRatio = TableUtils.getFieldValue("KATSAYI", "BIRIM", stockEntity, "STOKBIRI");
                String stockAmount = cursor.getString(cursor.getColumnIndex("MIKTAR"));
                gridArray.add(new Stock(stockName, stockNo, stockCode, stockKdvOranı, stockEntity, stockAmount, stockRatio, processNo));

                cursor.moveToNext();
            }
        }
    }
    cursor.close();
    gridAdapter = new AdapterStockGridListView(this, R.layout.stockgridlistitems, gridArray);
    gridView.setAdapter(gridAdapter);

}

我的适配器类在这里:

public class AdapterStockGridListView extends ArrayAdapter<Stock>
{
    Context context;
    int id;
    ArrayList<Stock> stock = new ArrayList<Stock>();

    public AdapterStockGridListView(Context context, int id, ArrayList<Stock> stock)
    {
        super(context, id, stock);
        this.id = id;
        this.context = context;
        this.stock = stock;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View row = convertView;
        RecordHolder holder = null;

        if (row == null)
        {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(id, parent, false);

            holder = new RecordHolder();
            holder.txtTitle = (TextView) row.findViewById(R.id.stockName);
            holder.txtStockNo = (TextView) row.findViewById(R.id.stockNo);
            holder.txtStockCode = (TextView) row.findViewById(R.id.stockCode);
            row.setTag(holder);
        }
        else
        {
            holder = (RecordHolder) row.getTag();
        }

        Stock item = stock.get(position);
        holder.txtTitle.setText(item.getStockName());
        holder.txtStockNo.setText(item.getStockNo());
        holder.txtStockCode.setText(item.getStockCode());

        return row;

    }

}

static class RecordHolder
{
    TextView txtTitle;
    TextView txtStockNo;
    TextView txtStockCode;

}
4

2 回答 2

1

这是因为您正在尝试访问已关闭的游标。所以删除这一行

 cursor.close();

并在您的活动或片段中正确管理光标

活动中

startManagingCursor(pass Your Cursor object here);

在片段中

getActivity().startManagingCursor(pass Your Cursor object here);
于 2013-09-03T08:23:19.087 回答
0

在适配器类中覆盖此方法并打印数组的大小可能只有一个值

@Override
public int getCount() {
    return stock.size();
}
于 2013-09-03T08:21:35.470 回答