【发布时间】:2014-11-25 13:41:04
【问题描述】:
我正在使用 SimpleCursorAdapter 在 ListView 中显示结果,但由于在搜索期间我必须多次查询我的数据库(使用 SearchView 小部件),因此我担心光标可能处于打开状态。
这是我查询数据库并在 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。
我已经看到几个问题,其中提到光标将自动关闭,但我想确保这是真的。
有没有关于这个的官方文档? SimpleCursorAdapter 真的会自动关闭光标吗??
提前致谢。
【问题讨论】:
-
通常您会在使用后关闭它,然后在您再次需要时再抓取一个。
-
但即使我在这部分
mResultsListView.setAdapter(cursorAdapter)之后关闭光标,ListView 也不会显示任何内容 -
通常你会在适配器上调用
notifyDataSetChanged,以确保在稍后的给定时间点构建它们的信息在屏幕上更新。但是你的结构中可能还缺少其他东西。 -
唯一有效的是不要尝试关闭光标。但是由于用户输入的每个字母都会调用
mDB.searchCustomersByName,所以我担心我可能会导致内存泄漏 -
如果在调用setAdapter后关闭它,那么适配器将没有任何数据可取,因为光标将被关闭。不再需要时关闭它。提示:只要是从光标显示数据,就需要打开光标。
标签: android simplecursoradapter searchview android-cursor