应用程序未关闭游标或在此处打开了数据库对象

时间:2018-10-06 09:26:34

标签: android sqlite cursor

我在我的Android应用程序中使用SQLite数据库。当我执行搜索操作时,随机发现此错误“应用程序未关闭游标”。此代码是否有问题?这是代码

    field.addTextChangedListener(new TextWatcher() {
                @Override
                public void afterTextChanged(Editable arg0) {

                }

                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

                }

                @Override
                public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
                    String test = s.toString().trim();
                    if (test.isEmpty()) {
                        if (myCursor != null)
                            myCursor.close();

                        myCursor = getReceipts1Data(orderTypes);
                    myAdapter = new CustomCursorAdapter(activity, myCursor, CursorAdapter.NO_SELECTION);
                    lView.setAdapter(myAdapter);

                    }
                }
            });


 public Cursor getReceipts1Data(Global.OrderType[] orderTypes) 

    {
        String subquery1 = "SELECT ord_id as _id,ord_total,ord_issync,cust_id,isVoid,ord_type" +
                " FROM Orders WHERE ord_type IN (";
        String subquery2 = ") AND isOnHold = '0' ORDER BY rowid DESC";
        Cursor cursor = DBManager.getDatabase().rawQuery(subquery1 + getOrderTypesAsSQLArray(orderTypes) + subquery2, null);
        cursor.moveToFirst();
        return cursor;
    }

1 个答案:

答案 0 :(得分:0)

如果它不为空,我可以看到您关闭游标的那一行,但是如果它不为空,我就看不到您正在关闭它的那一行。更具体地说,您是否尝试添加

myCursor.close();

在行之后:

lView.setAdapter(myAdapter);

在onTextChanged方法中