2012-01-02 9 views
0

私はフィールドとして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] 
     } 

    } 
} 
+0

をonPreExecute()でunclickableとonPostExecute()で再びクリック可能にする –

+0

こんにちはmidoalageb - 素晴らしい質問、私は明らかに私の元の投稿で十分ではなかった。このボタンの名前は「Save&Next」です。データが保存されると、リストは次のレコードでリロードされます。それは、顧客とその注文のリストを踏むようなものです。ページがリロードされると、セーブ&ネクストボタンがクリック可能になります。 – Kevin

+0

複数のスレッドが同じ変数に同時にアクセスするのを防ぐために同期文を試してください –

答えて

0

;:ここ

は、私が何をしようとしているのストリップダウンバージョンです。 }

} 

}

connector.close()。 }

これを試してください。

+0

こんにちはRashmi - onPostExecute()メソッドのconnector.close()**を**外に移動することをお勧めしますか? – Kevin

+0

はい。それはどちらかといえば、DBオープンクローズマターの最善のアプローチはtry、catch、finallyブロックを使用することです。 finallyブロックが実行されていることを確信しているので、finallyブロックにconnector.close()を入れてください。あなたの問題は解決されます –

+0

こんにちは、Rashmi - どちらの提案も機能しませんでした。私は@Kevin Galliganのhttp://www.touchlab.co/blog/single-sqlite-connection/からこの記事のいくつかを試してみようと思います。私はステータスを更新します。 – Kevin

0

コネクターの周りにラッパーを同期してシングルトンとして作成します。私はあなたが同時にデータベースにアクセスしていると信じています。これは私自身の1の剥奪バージョンです:

(私は外部キーを有効にするgetWriteableデータベースをオーバーライドしていますが、あなたはあまりにも行うことを持っていけない)なぜあなたは単に保存]ボタンをしない

 public class DatabaseHelper extends SQLiteOpenHelper { 

    private static final String DATABASE_NAME = "myDatabase"; 
    private static final int DATABASE_VERSION = 1; 
    private Context context = null; 
    private static DatabaseHelper instance = null; 

    public static synchronized DatabaseHelper getInstance(Context context){ 

     if (instance == null){ 
      instance = new DatabaseHelper(context, null);   
     } 

     return instance; 
    } 

    @Override 
    public synchronized SQLiteDatabase getWritableDatabase() { 
     SQLiteDatabase db = super.getWritableDatabase(); 
     db.execSQL("PRAGMA foreign_keys=ON;"); 
     return db; 
    } 

    private DatabaseHelper(Context context, CursorFactory factory) { 
     super(context, DATABASE_NAME, factory, DATABASE_VERSION); 
     this.context = context; 
    } 


} 
関連する問題