如何从非UI线程更新游标?

时间:2016-11-09 02:19:15

标签: android android-sqlite refresh android-cursoradapter android-cursor

我正在使用游标初始化适配器,并将适配器设置为listview。

DatabaseHelper dbh = new DatabaseHelper(getActivity());
Cursor c = dbh.getMoviesTableCursor();
myAdapter =  new DatabaseAdapter(getActivity(), c, 0);
listView.setAdapter(myAdapter);

当我在dbh.getMoviesTableCursor()方法中进行查询时,我在单独的线程中运行所述查询

private Cursor getMoviesTableCursor(){

    ...

    new Thread() {
        @Override
        public void run() {
            moviesTableCursor = qb.query(db, sqlSelect, null, null, null, null, null);

        }
    }.start();

    ...

    return moviesTableCursor;
}

现在我的listview没有显示任何内容,相比之下我没有在新线程中运行查询

private Cursor getMoviesTableCursor(){

    ...


            moviesTableCursor = qb.query(db, sqlSelect, null, null, null, 

    ...

    return moviesTableCursor;
}

所以我尝试使用notifyDataSetChanged()

的处理程序更新cusor
final Handler handler = new Handler();
private Cursor getMoviesTableCursor(){

    ...

    new Thread() {
        @Override
        public void run() {

            moviesTableCursor = qb.query(db, sqlSelect, null, null, null, null, null);

            handler.post(new Runnable() {
                @Override
                public void run() {

                    // use handler to avoid update UI from none-UI thread.
                    // do your update list view adapter here.
                    // FragmentMovies.myAdapter.notifyDataSetChanged();

                    MyActivity.myAdapter.notifyDataSetChanged();
                }
            });

        }
    }.start();

    ...

    return moviesTableCursor;
}

notifyDataSetChanged()不会刷新myAdapter

中的光标

0 个答案:

没有答案