Reputation: 5400
How do i update listivew when async task is done. Below is the sample code but the listview isn't updated.
class CallXML extends AsyncTask<Void, Void, Void> {
int gcid;
int scid;
public CallXML(int gid, int sid) {
// TODO Auto-generated constructor stub
gcid = gid;
scid = sid;
}
protected void onPreExecute() {
}
protected Void doInBackground(Void... arg0) {
// here goes the xml parsing....
}
return null;
}
protected void onPostExecute(String result) {
Log.e("TAG", "In postExecute");
Cursor cur3 = database2.query("Quote", qfield, null, null, null, null, null);
cur3.moveToFirst();
do {
quotesarray.add(cur3.getString(2));
} while (cur3.moveToNext());
if(cur3 != null){
cur3.close();
}
QList.post(new Runnable() {
public void run() {
mAdapter = new CustomAdapter();
mAdapter.notifyDataSetChanged();
QList.setAdapter(mAdapter);
}
});
if (helper2 != null) {
helper2.close();
}
if (database2 != null) {
database2.close();
}
}
}
EDIT:
Acutally onPostExecute
is not executed why..This is the way I call asynctask new CallXML(gcid, scid).execute();
Upvotes: 0
Views: 2713
Reputation: 2524
Also, onPostExecute
is on the main thread so should not be doing database queries there. Instead, get data in doInBackground
and return the final collection from there.
onPostExecute
can be used for UI updates and updating your adapter with result collection.
Edit: posting a runnable
QList.post(new Runnable() {
public void run() {
//mAdapter.notifyDataSetChanged();
QList.setAdapter(mAdapter);
}
});
is not required since you are in the main loop.
Upvotes: 2
Reputation: 1
Did u got any error? if so please post it. If not Check the size of data you got from database and if you want to refresh listview just call listview.invalidateViews() it will refresh the list view and set the new data in the listview.
Upvotes: 0
Reputation: 68187
You are not providing string items to the adapter in your code. And you don't need to call notifyDataSetChanged when you are setting adapter to a list, because when you set an adapter, it automatically loads the data into list. Perhaps you me try doing it in this way:
protected void onPostExecute(String result) {
Log.e("TAG", "In postExecute");
Cursor cur3 = database2.query("Quote", qfield, null, null, null, null, null);
cur3.moveToFirst();
mAdapter = new CustomAdapter();
do {
mAdapter.add(cur3.getString(2));
} while (cur3.moveToNext());
if(cur3 != null){
cur3.close();
}
QList.post(new Runnable() {
public void run() {
//mAdapter.notifyDataSetChanged();
QList.setAdapter(mAdapter);
}
});
if (helper2 != null) {
helper2.close();
}
if (database2 != null) {
database2.close();
}
}
Upvotes: 2