【问题标题】:ListView SimpleCursorAdapter Updated AsynchronouslyListView SimpleCursorAdapter 异步更新
【发布时间】:2012-01-02 05:58:48
【问题描述】:

我有一个将 SimpleCursorAdapter 作为字段的类。该适配器用于提供具有 viewBinder 的列表视图。

我有一个运行的异步任务,它向数据库添加一个条目,然后更新游标。

在测试中,如果我在运行异步进程的按钮上单击的太快,我会收到错误:

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]
        }

    } 
}

【问题讨论】:

  • 为什么不简单地在 onPreExecute() 中使 Save 按钮不可点击,并在 onPostExecute() 中再次使其可点击
  • 嗨 midoalageb - 好问题,我在原始帖子中显然不够清楚。该按钮的标题为“保存并下一步”。保存数据后,视图会重新加载列表中的下一条记录。这就像逐步浏览客户及其订单的列表。当页面重新加载时,保存&next按钮是可点击的。
  • 尝试使用Synchronized语句防止多个线程同时访问同一个变量

标签: android sqlite android-asynctask simplecursoradapter


【解决方案1】:

if (result.MyCursor != null) { 适配器.changeCursor(result.MyCursor); }

}

}

connector.close(); }

试试这个。

【讨论】:

  • 嗨,Rashmi - 你是不是建议我将onPostExecute() 方法的connector.close() 移出
  • 是的..如果这也不起作用,那么 DB 打开关闭问题的最佳方法是使用 try、catch 和 finally 块。您确定 finally 块已执行,因此将 connector.close() 放入 finally 块中。您的问题将得到解决
  • 你好,Rashmi - 这两个建议都没有奏效。我将从@Kevin Galligan 的touchlab.co/blog/single-sqlite-connection 帖子中尝试一些事情,我会更新状态。
【解决方案2】:

在连接器周围制作一个包装器,同步并作为单例。我相信您正在同时访问数据库。这是我自己的一个精简版:

(我正在覆盖 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;
    }


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    • 2015-11-24
    • 2017-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多