multithreading - Using AsyncTask to update UI in Android -
i'm trying use asynctask read 1000 lines sqlite. i'm using progressdialog show progress. working fine , when method onpostexecute() dismiss dialog , try update entire lines on main thread. dialog disappears ui takes lot update. during doinbackground() method, i'm filling tablelayout , return onpostexecute(). how can show progressdialog until main thread totally filled?
private class filltable extends asynctask<void, integer, tablelayout>{ private file file; private progressdialog pleasewaitdialog; private context ctx; private int colcount; private int lincount; private scrollview scroolview; private sqlitedatabase db; private cursor cursor; public filltable(context context, string newquery){ ctx = context; scroolview = (scrollview)serviceactivity.this.findviewbyid(r.id.sv_service_vertical); file = new file(environment.getexternalstoragedirectory().getabsolutepath() + getresources().getstring(r.string.database_path), getresources().getstring(r.string.database_name)); if(!file.exists()) { toast.maketext(ctx, "base de dados inválida ou não existe!", toast.length_short).show(); this.cancel(true); } db = sqlitedatabase.openorcreatedatabase(file, null); cursor = db.rawquery(newquery, null); pleasewaitdialog = new progressdialog(ctx); pleasewaitdialog.setcancelable(false); pleasewaitdialog.setprogressstyle(progressdialog.style_horizontal); } @override protected void onpreexecute(){ scroolview.removeallviews(); colcount = cursor.getcolumncount(); lincount = cursor.getcount(); pleasewaitdialog.setmax(lincount); pleasewaitdialog.settitle("a ler base de dados"); pleasewaitdialog.setmessage("por favor aguarde..."); pleasewaitdialog.show(); textview tv = new textview(serviceactivity.this); tv.setgravity(gravity.center_horizontal); tv.settext("a apresentar resultados.\npode demorar algum tempo!\n\npor favor aguarde..."); ((scrollview)serviceactivity.this.findviewbyid(r.id.sv_service_vertical)).addview(tv); ((textview)serviceactivity.this.findviewbyid(r.id.tv_service_num_entradas)).settext(""); } @override protected tablelayout doinbackground(void... params) { tablelayout tablelayout = null; if(cursor.movetofirst()) { tablelayout = new tablelayout(serviceactivity.this); //preenchimento dos nomes das colunas textview textview; tablerow tablerow = new tablerow(serviceactivity.this); (int = 0; < colcount; i++) { textview = new textview(serviceactivity.this); if (i == 0) textview.settext(cursor.getcolumnname(i)); else textview.settext("\t" + cursor.getcolumnname(i)); textview.settextcolor(color.magenta); tablerow.addview(textview); } tablelayout.addview(tablerow); //preenchimento das restantes linhas e das colunas de cada linha int k = 1;//a contar com o cabeçalho { this.publishprogress(k); tablerow = new tablerow(serviceactivity.this); (int = 0; < colcount; i++) { textview = new textview(serviceactivity.this); textview.settextsize(11); textview.settextcolor(color.white); string str = cursor.getstring(i); if(str == null) { if (i == 0) textview.settext("..."); else textview.settext("\t" + "..."); } else{ if (i == 0) textview.settext(str); else textview.settext("\t" + str); } textview.settextcolor(color.white); tablerow.addview(textview); } tablelayout.addview(tablerow); k++; } while (cursor.movetonext()); } db.close(); cursor.close(); return tablelayout; } @override protected void onprogressupdate(integer... progress){ pleasewaitdialog.setprogress(progress[0].intvalue()); } @override protected void onpostexecute(final tablelayout tl){ scroolview.post(new runnable() { @override public void run() { scroolview.removeallviews(); scroolview.addview(tl); ((textview)serviceactivity.this.findviewbyid(r.id.tv_service_num_entradas)).settext("nº de entradas: " + string.valueof(lincount)); } }); if(pleasewaitdialog.isshowing()) pleasewaitdialog.dismiss(); } @override protected void oncancelled(final tablelayout tl){ if(pleasewaitdialog.isshowing()) pleasewaitdialog.dismiss(); ((textview)serviceactivity.this.findviewbyid(r.id.tv_service_num_entradas)).settext("nº de entradas: 0"); scroolview.removeallviews(); } }
you have been used asynctask update ui thread -> fine. seem used asynctask wrong way.
if want display progress dialog util main thread (ui thread) filled:
define: [yourclasstodotask] extends asynctask<[valueinput], [valueupdate], [valueoutput]>
[valueupdate] : object of line want update ui.
use 3 methods: doinbackground, onprogressupdate, onpostexecute
- doinbackground: long task in background data source, update main thread method publishprogress([valueupdate])
onprogressupdate: update value ui thread got after doinbackground called publishprogress([valueupdate])
onprogressupdate(integer... progress) -> [valueupdate] array or single object
if array: [valueupdate] = progress
if single object: [valueupdate] = progress[0]
onpostexecute: dismis progressdialog (in here, ui thread total filled)