【问题标题】:gridview didn't work second timesgridview 没有第二次工作
【发布时间】:2013-09-03 08:05:33
【问题描述】:

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

logCat:android.database.CursorWindowAllocationException:2048 kb 的光标窗口分配失败。 # Open Cursors=761 (# cursors open by this proc=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;

}

【问题讨论】:

    标签: android gridview cursor


    【解决方案1】:

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

     cursor.close();
    

    要正确管理光标,请在您的活动或片段中写入这一行

    活动中

    startManagingCursor(pass Your Cursor object here);
    

    在片段中

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-21
        • 2011-01-16
        • 1970-01-01
        • 1970-01-01
        • 2017-04-14
        • 2015-03-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多