我想使用AsyncTask填充我的ListView。在评论使用AsyncTask填充ListView(doInBackground()返回光标)
private class DownloadDataTask extends AsyncTask<String, Void, Cursor> {
private final ProgressDialog dialog = new ProgressDialog(ctx);
@Override
protected void onPreExecute() {
this.dialog.setMessage(ctx.getString(R.string.AcquiringData));
this.dialog.show();
}
@Override
protected Cursor doInBackground(final String... args) {
DbAdapter dbAdapter = new DbAdapter(ctx);
dbAdapter.open();
// normally when I call this method from main class it reurns cursor full of values
Cursor cursor = dbAdapter.fetchAllItemsInExpenses();
return cursor;
}
protected void onPostExecute(final Cursor cursor) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
//cursor is empty
}
ctx
包含的问题是,设置在主类的OnCreate()
按照要求粘贴fetchAllItemsInExpenses()方法上下文:
public Cursor fetchAllItemsInExpenses() {
String query = "SELECT ... " //(some terribly boring and long query string, but trust me, it works perfectly fine)
return SQLDb.rawQuery(query, null);
}
检查使用'如果(空!=光标){cursor.getCount()}',并检查你的方法'fetchAllItemsInExpenses'是正确的还是不 –
灿你显示'fetAllItemsInExpenses()'源?另外,你有没有考虑过使用['CursorLoader'](http://developer.android.com/reference/android/content/CursorLoader.html)? –
当我从Activity类调用它时,fetchAllItemsInExpenses()正常工作。不过花了一些时间,所以我决定把列表填入AsyncTask。但是提供了代码。 @JasonRobinson纠正我如果我错了,Cursor加载器是一种专为加载游标而设计的任务吗? –