multithreading - recyclerView.adapter on notifydatasetchanged hangs the ui android -
this question has answer here:
i having trouble updating recyclerview notify data set changed, since hangs ui couple of seconds upon called. after searching on net, suggested update adapter on background thread.
private synchronized void updateadapter() { asynctask<void, void, void> task = new asynctask<void, void, void>() { @override public void doinbackground(void... params) { adapter.notifydatasetchanged(); return null; } }; task.execute(); }
and upon running line of code, app crashed error,
"only original thread created view hierarchy can touch views."
and upon searching further, suggested solution run on ui thread.
runonuithread(new runnable() { @override public void run() { } });
which brings me initial problem. how should handle problem? appreciated. thank you.
private synchronized void updateadapter() { asynctask<void, void, void> task = new asynctask<void, void, void>() { @override public void doinbackground(void... params) { adapter.notifydatasetchanged(); return null; } }; task.execute(); }
please study asynctask doinbackground
should not have ui changes .in case u performing ui changes on seperate thread in asynctask . put notifydatasetchanged on onpostexecute shown below( works on main thread ). add onpreexcute()
, onpostexecute()
performed on main ui , onbackground()
on separate thread ( not main ui ).
private synchronized void updateadapter() { asynctask<void, void, void> task = new asynctask<void, void, void>() { @override public void doinbackground(void... params) { // query db or perform long operations here return null; } @override onpostexecute(void.. params){ adapter.notifydatasetchanged(); } }; task.execute(); }
Comments
Post a Comment