可搜索的字典示例内容提供程序CursorLeak

时间:2014-01-20 08:19:50

标签: android cursor android-sqlite android-contentprovider

我是否知道为什么每次我在搜索管理器上输入内容然后按下去时我都会收到此错误?当我按下GO按钮时,下面的这条消息总是出现在我的第5个查询中。为什么?根据我从这个链接中读到的内容,Closing the database in a ContentProvider

如果我使用的是ContentProvider,则无需关闭数据库,但为什么我会继续收到此消息?是因为我错过了什么吗?

01-20 15:05:34.843: E/CursorLeakDetecter(16689): PossibleCursorLeak:content://com.example.android.searchabledict.DictionaryProvider/dictionary,QueryCounter:5

============================================

private void showResults(String query) {

    Cursor cursor = managedQuery(DictionaryProvider.CONTENT_URI, null, null,
                            new String[] {query}, null);

    if (cursor == null) {
        // There are no results
        mTextView.setText(getString(R.string.no_results, new Object[] {query}));
    } else {
        // Display the number of results
        int count = cursor.getCount();
        String countString = getResources().getQuantityString(R.plurals.search_results,
                                count, new Object[] {count, query});
        mTextView.setText(countString);

        // Specify the columns we want to display in the result
        String[] from = new String[] { DictionaryDatabase.KEY_WORD,
                                       DictionaryDatabase.KEY_DEFINITION };

        // Specify the corresponding layout elements where we want the columns to go
        int[] to = new int[] { R.id.word,
                               R.id.definition };

        // Create a simple cursor adapter for the definitions and apply them to the ListView
        SimpleCursorAdapter words = new SimpleCursorAdapter(this,
                                      R.layout.result, cursor, from, to);
        mListView.setAdapter(words);

        // Define the on-click listener for the list items
        mListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // Build the Intent used to open WordActivity with a specific word Uri
                Intent wordIntent = new Intent(getApplicationContext(), WordActivity.class);
                Uri data = Uri.withAppendedPath(DictionaryProvider.CONTENT_URI,
                                                String.valueOf(id));
                wordIntent.setData(data);
                startActivity(wordIntent);
            }
        });
    }
}

0 个答案:

没有答案