gridview没有第二次工作

时间:2013-09-03 08:05:33

标签: android gridview cursor

我有一个像listview一样的gridview。它首次运行正常,但是当按下并返回具有gridView的活动时,会出现一些错误......

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

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

我的gridView代码:

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;

}

2 个答案:

答案 0 :(得分:1)

这是因为您正在尝试访问关闭的光标。请删除此行

 cursor.close();

要管理游标,请在活动或片段中正确写下此行

活动

startManagingCursor(pass Your Cursor object here);

在片段中

getActivity().startManagingCursor(pass Your Cursor object here);

答案 1 :(得分:0)

在适配器类中重写此方法,并且还打印数组的大小,可能只有一个值

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