performance - Android : how to implement PullToRefreshListView in proper way -
i new android , start learning how implement pulltorefreshlistview using library of chrisbanes. can guys please explain me, should put code call api getting data, , part should set the(imagebitmap) after data (image url) api. know, should in background avoid ui freeze when loading image ui, not sure. please help.
the following sample code library: please explain me should in getdatatask , onpostexecute. in case loading image.
@override public void onrefresh(pulltorefreshbase<listview> refreshview) { // work refresh list here. new getdatatask().execute(); } private class getdatatask extends asynctask<void, void, string[]> { @override protected string[] doinbackground(void... params) { // simulates background job. try { thread.sleep(4000); } catch (interruptedexception e) { } return mstrings; } @override protected void onpostexecute(string[] result) { mlistitems.addfirst("added after refresh..."); madapter.notifydatasetchanged(); // call onrefreshcomplete when list has been refreshed. mpullrefreshlistview.onrefreshcomplete(); super.onpostexecute(result); } }
sorry newbie question, jsust want confirm in order follow standard. sorry bad english
i think should put getdatatask in separated class listener. example of listener can have:
public abstract class remotecalllistener implements iremotecalllistener { @override public abstract void onstring(string s); //string example. @override public abstract void onerror(exception e); }
you should give listener constructor if async task.
private class getdatatask extends asynctask<void, void, string[]> { remotecalllistener listener; public getdatatask(){ } public getdatatask(remotecalllistener listener){ this.listener = listener; } @override protected string[] doinbackground(void... params) { // simulates background job. try { thread.sleep(4000); } catch (interruptedexception e) { } return mstrings; } @override protected void onpostexecute(string[] result) { listener.onstring(result); // mlistitems.addfirst("added after refresh..."); // madapter.notifydatasetchanged(); // call onrefreshcomplete when list has been refreshed. // mpullrefreshlistview.onrefreshcomplete(); super.onpostexecute(result); } }
to make instance of call should like
getdatatask task = new getdatatask(yourlistener); task.execute("your link");
and in 'controller'class should make instance of remotecalllistener , when onstring called:
mlistitems.addfirst("added after refresh..."); madapter.notifydatasetchanged(); // call onrefreshcomplete when list has been refreshed. mpullrefreshlistview.onrefreshcomplete();
you can freeze ui dialogs class android. example given here:
public final static progressdialog showloading(context c, string title, string message, boolean indeterminate) { progressdialog p = new progressdialog(c); p.settitle(title); p.setmessage(message); p.setcancelable(false); p.setindeterminate(indeterminate); if (!indeterminate) { // p.setprogressdrawable( c.getresources().getdrawable( // r.drawable.progress ) ); p.setprogressstyle(progressdialog.style_horizontal); p.setprogress(0); p.setmax(100); } p.show(); return p; }
but dont forget close dialog!
you can contact me anytime if have further questions
Comments
Post a Comment