我有一個具有SimpleCursorAdapter作爲字段的類。該適配器用於提供具有viewBinder的列表視圖。ListView SimpleCursorAdapter異步更新
我有一個異步任務,運行它將一個條目添加到數據庫,然後更新遊標。
在測試中,如果我點擊太快上運行的異步過程中的按鈕,我得到一個錯誤:
java.lang.RuntimeException: An error occured while executing doInBackground()
...
Caused by: java.lang.IllegalStateException: database [path_to_my_db] already closed
代碼工作完美 - 除非......用戶點擊保存按鈕迅速繼任...我是新手,所以任何輸入將不勝感激!
這裏是什麼,我試圖做一個精簡版:
public class MyActivity extends Activity {
private DatabaseConnector connector; // this is my class for managing SQLite
private SimpleCursorAdapter adapter;
....
@Override
public void onCreate(Bundle savedInstanceState){
...
myListView = (ListView)findViewById(R.id.my_list_view);
String[] = new String{"This", "part", "is", "working"};
int[] to = new int[] {1,2,3,4}; // again, this is working...
adapter = new SimpleCursorAdapter(this, R.layout.my_list_item_row, null, from, to);
adapter.setViewBinder(new ViewBinder(){
... // this is all working
... // the viewBinder is for custom date formatting... again, all works
});
myListView.setAdapter(adapter);
}
private class MyAsyncTask extends AsyncTask<Context, Void, ExerciseInstanceViewModel>{
MyViewModel vm; // this viewModel has a cursor member...
public MyAsyncTask([variables-all-working]){
}
@Override
protected MyViewModel doInBackground(Context... params) {
connector = new DatabaseConnector(MyActivity.this);
connector.open(); // TODO: Getting 'did not close database error here...'
vm = connector.runMethodThatIncludesCursorInReturnType([input-paramters-working]);
return vm;
}
// use the cursor returned from the doInBackground method
@Override
protected void onPostExecute(MyViewModel result){
super.onPostExecute(result);
// set instance fields in outer class...;
// set textView, progressBar, etc..
if (result.MyCursor != null)
{
adapter.changeCursor(result.MyCursor);
}
connector.close(); // aren't i closing the db here???
[Code to reload page with next detail items]
}
}
}
你爲什麼不乾脆讓保存按鈕在onExExcute()中不可點擊並使其在onPostExecute()中再次可點擊 – 2012-01-04 12:22:51
嗨midoalageb - 好問題,我的原始帖子顯然不夠清晰。該按鈕標題爲「保存並下一步」。數據保存後,視圖將重新加載列表中的下一條記錄。這就像是逐步瀏覽客戶和訂單列表。當頁面重新加載時,保存&下一步按鈕是可點擊的。 – Kevin 2012-01-04 22:28:00
嘗試使用Synchronized語句來防止多個線程同時訪問相同的變量 – 2012-01-04 22:49:08