i've started programming android (from ios), , working boiler plate code adt generates me when choose activity+scrollable tabs option in new project wizard. in code below i've marked same in comments.
the scenario enter text in edittext, press button , fetch data webservice. wish put data in appropriate fragment (there 3 fragments) inside view pager. getting data , able set on fragment 2 , fragment 3 when swipe, data relevant fragment 1 not getting set once asyctask complete. however, after swiping fragment 3, when come fragment 1, data there.
here code:
activity
public class xxxx extends fragmentactivity { sectionspageradapter msectionspageradapter; viewpager mviewpager; //my additions boiler plate source generated adt public arraylist<string> p1t,p2t,p3t,p4t; private edittext edittext; private button button; private boolean flag; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.konjugation); msectionspageradapter = new sectionspageradapter (getsupportfragmentmanager()); mviewpager = (viewpager) findviewbyid(r.id.pager); mviewpager.setadapter(msectionspageradapter); //my additions boiler plate source below flag=false; p1t = new arraylist<string>(6); p2t = new arraylist<string>(6); p3t = new arraylist<string>(6); p4t = new arraylist<string>(6); edittext = (edittext) findviewbyid(r.id.edittext1); button = (button) findviewbyid(r.id.button1); button.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { string url = edittext.gettext().tostring(); if(!url.isempty()) { url="http://.../?q="+edittext.gettext().tostring(); pasynctask task = new pasynctask(); task.execute(url); } mviewpager.setonpagechangelistener(new viewpager.simpleonpagechangelistener(){ @override public void onpageselected(int pos) { if(flag==true) { if(pos==0) dummysectionfragment.settext(p4t,p1t); if(pos==1) dummysectionfragment.settext(p4t,p2t); if(pos==2) dummysectionfragment.settext(p4t,p3t); } else { log.d("ts","data not arrived"); } } }); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.konjugation, menu); return true; }
pageradapter
public class sectionspageradapter extends fragmentstatepageradapter { public sectionspageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { fragment fragment = new dummysectionfragment(); return fragment; } @override public int getcount() { return 3; } @override public charsequence getpagetitle(int position) { switch (position) { case 0: return getstring(r.string.title_section1); case 1: return getstring(r.string.title_section2); case 2: return getstring(r.string.title_section3); } return null; } @override public void notifydatasetchanged() { int pos = mviewpager.getcurrentitem(); if(pos==0) dummysectionfragment.settext(p4t,p1t); else if(pos==1) dummysectionfragment.settext(p4t,p2t); else if(pos==2) dummysectionfragment.settext(p4t,p3t); } }
asynctask
private class pasynctask extends asynctask<string, void, string> { @override protected string doinbackground(string... params) { string response = ""; (string url : params) { defaulthttpclient client = new defaulthttpclient(); httpget httpget = new httpget(url); try { httpresponse execute = client.execute(httpget); inputstream content = execute.getentity().getcontent(); bufferedreader buffer = new bufferedreader(new inputstreamreader(content)); string s = ""; while ((s = buffer.readline()) != null) { response += s; } } catch (exception e) { e.printstacktrace(); } } return response; } protected void onpostexecute(string result) { try { p1t.clear(); p2t.clear(); p3t.clear(); //do important result here flag=true; //hoping somehow viewpager adapter refresh view msectionspageradapter.notifydatasetchanged(); } catch (exception e) { e.printstacktrace(); } } }
viewpager's fragment
public static class dummysectionfragment extends fragment { private static textview dummytextview; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_konjugation_dummy, container, false); dummytextview = (textview) rootview.findviewbyid(r.id.section_label); return rootview; } public static void settext(arraylist<string> s, arraylist<string> t) { try { dummysectionfragment.dummytextview.settext(""); for(int i=0;i<s.size();i++) { dummysectionfragment.dummytextview.append(s.get(i)+" "+t.get(i)+"\n\n"); } } catch(nullpointerexception e) { e.printstacktrace(); } } } }
i feel kinda lost, after reading threads here. please :-(
you should update inside of runonuithread() method, takes runnable object argument, can pass implementation in it.
Comments
Post a Comment