私はフィールドとして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
コードは完璧に動作します - ユーザーが急速に保存ボタンをクリックしない限り...継承...私はこれのすべてに新しいので、任意の入力が大いに感謝される! (!result.MyCursor = NULL){ adapter.changeCursor(result.MyCursor)場合
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]
}
}
}
をonPreExecute()でunclickableとonPostExecute()で再びクリック可能にする –
こんにちはmidoalageb - 素晴らしい質問、私は明らかに私の元の投稿で十分ではなかった。このボタンの名前は「Save&Next」です。データが保存されると、リストは次のレコードでリロードされます。それは、顧客とその注文のリストを踏むようなものです。ページがリロードされると、セーブ&ネクストボタンがクリック可能になります。 – Kevin
複数のスレッドが同じ変数に同時にアクセスするのを防ぐために同期文を試してください –