Reputation: 257
I have written an Asynctask to fetch data from db and return cursor. However, when I call it in my main thread and pass it to a cursor, Android studio gives me an error. I've done all this as per the android dev docs, I'm unable to understand what I'm doing wrong.
Here's my asynctask:
public class FetchDataDay extends AsyncTask<DatabaseContract, Cursor, Cursor> {
@Override
protected Cursor doInBackground(DatabaseContract... params) {
Cursor cursor = null;
try {
cursor = params[0].getSteps(System.currentTimeMillis());
} catch (ParseException e) {
e.printStackTrace();
}
return cursor;
}
@Override
protected void onPostExecute(Cursor cursor) {
super.onPostExecute(cursor);
}
}
And here I am calling the task:
public void initday() {
Cursor cursor = new FetchDataDay().execute(databaseHandler);
// this line is giving me an error saying its the wrong return type.
if (cursor.getCount() != 0) {
cursor.moveToFirst();
do {
//doing something with the data
} while (cursor.moveToNext());
}
}
Upvotes: 1
Views: 665
Reputation: 87
Just put all the work of UI thread into your onPostExecute, it should work.
Upvotes: 0
Reputation: 132982
Cursor cursor = new FetchDataDay().execute(databaseHandler);
this line is giving me an error saying its the wrong return type.
As see here:
AsyncTask.execute(Params...) : method return type is void
instead of Cursor
type.
Use onPostExecute
for updating UI According to doInBackground
method result.
And if FetchDataDay
is separate class then create a custom event listener using interface
which will notify in caller class when onPostExecute
method is called.
Upvotes: 2