我正在使用游标初始化适配器,并将适配器设置为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()
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