何时关闭在SimpleCursorAdapter中使用的Cursor [英] When to close Cursor used in SimpleCursorAdapter

查看:87
本文介绍了何时关闭在SimpleCursorAdapter中使用的Cursor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SimpleCursorAdapter在ListView中显示结果,但是由于在搜索过程中(使用SearchView小部件)必须查询数据库很多次,因此我担心光标可能保持打开状态.

I'm using a SimpleCursorAdapter to display results in a ListView but since I've got to query my database lots of times during a search (using the SearchView widget) it worries me that the cursor might be left opened.

这是我查询数据库并在ListView中显示结果的方式:

This is how I query my database and show the results in a ListView:

class SearchCustomers extends AsyncTask<String,Void,Cursor>{

        @Override
        protected Cursor doInBackground(String... params) {         
            //get the query
            String query=params[0].toLowerCase(Locale.getDefault());
            Cursor cursor=mDB.searchCustomersByName((query != null ? query : "@@@@"));
            return cursor;

        }

        @Override
        protected void onPostExecute(Cursor result) {           

            if (result != null) {

                String[] from = new String[] { QuickOrderDB.ID,
                        QuickOrderDB.NAME,
                        QuickOrderDB.ADDRESS,
                        QuickOrderDB.PHONE_NUMBER };

                int[] to = new int[] { R.id.customerIDTextView,
                        R.id.customerNameTextView,R.id.customerAddressTextView ,
                        R.id.customerPhoneTextView };

                SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(SearchCustomersActivity.this,
                        R.layout.results_customer_item, result, from, to);
                mResultsListView.setAdapter(cursorAdapter);                 

            }
        }           

    }   

我已经尝试了很多方法来关闭游标,但是即使我在 mResultsListView.setAdapter(cursorAdapter); 之后关闭它,结果也始终是相同的:一个空的ListView >.

I have tried many things to close the cursor, but even If I close it after mResultsListView.setAdapter(cursorAdapter); the result is always the same: an empty ListView.

我已经看到了几个问题,其中提到游标将自动关闭,但是我想确保这是正确的.

I've already seen a couple of questions in which it is mentioned that the cursor will be closed automatically, but I want to make sure this is true.

是否有关于此的官方文档?SimpleCursorAdapter真的会自动关闭光标吗??

谢谢.

推荐答案

  1. 完成操作后,需要关闭光标.在setAdapter()调用之后关闭它会阻止适配器访问数据.因此,在当前活动拆除生命周期阶段(例如onPause()或onStop())期间,关闭光标的更好位置.(不应该使用onDestroy(),因为Android运行时不能保证会调用它.我认为可以保证最新版本的onStop()可以使用)
  2. 我认为SimpleCursorAdapter适配器不会自动关闭光标.官方文档提到 changeCursor()自动关闭旧光标,因此另一种选择是在搜索后更改光标. http://developer.android.com/reference/android/widget/CursorAdapter.html#changeCursor(android.database.Cursor)
  1. You need to close your cursor once you are done with it. Closing it after setAdapter() call would prevent the adapter from accessing the data. Hence a better place to close the cursor would be during current activities tear down life cycle stages such as onPause() or onStop(). (onDestroy() should not be used as Android run-time does not guarantee calling it. I think on latest version onStop() is guaranteed)
  2. I don't think SimpleCursorAdapter adapter automatically closes the cursor automatically. The official document mentions that changeCursor() automatically closes the old cursor, so another option could be to change your cursor after search. http://developer.android.com/reference/android/widget/CursorAdapter.html#changeCursor(android.database.Cursor)

这篇关于何时关闭在SimpleCursorAdapter中使用的Cursor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆