Communicating UI Thread not usings View.post in android? -
Communicating UI Thread not usings View.post in android? -
i have question when comes views can utilize view.post
illustration how in adapter adapter.notifydatasetchanged()
cant utilize view.post,
example im updating listview more best practice of using codes
madapter = new homepageadapter(this, list); mlistview.setadapter(madapter); madapter.notifydatasetchanged();
which of style best practice:
runonuithread
madapter = new homepageadapter(this, list); homepage.this.runonuithread(new runnable() { @override public void run() { mlistview.setadapter(madapter); madapter.notifydatasetchanged(); } });
asynctask
class updates extends asynctask<void, void, void> { @override protected void doinbackground(void... arg0) { madapter = new homepageadapter(homepage.this, defaultlistval); homecoming null; } @override protected void onpostexecute(void result) { super.onpostexecute(result); mlistview.setadapter(madapter); madapter.notifydatasetchanged(); } }
handler
handler = new handler() { public void handlemessage(message msg) { mlistview.setadapter(madapter); madapter.notifydatasetchanged(); } }; class update implements runnable { @override public void run() { madapter = new homepageadapter(homepage.this, defaultlistval); handler.sendemptymessage(0); } }
each of them have different functionality:
which of style best practice:
runonuithread
use when if on different thread because thread create view can call/update view, if updating on different thread without calling parent thread calledfromwrongthreadexception
asynctask
use when doing network connection, hard work on cpu, etc. block main thread.
handler
same runonuithread
phone call main thread, handler
called parent thread instantiate it.
android android-activity android-asynctask android-handler
Comments
Post a Comment