android - public static variable has the value , but when i try to show it to textview(in another class) it has no value -
my activity flow , login page->mainpage(in have button go userprofile has onclick method)-> button onclick method "startactivity(userprofile)" have onclick
method loads activity userprofile
. want load details of user, have static
variables no luck.
public class userprofile extends appcompatactivity { textview textview , textview1 , textview2 ; context context_userprofile; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_userprofile); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); func_userprofile_show(); } public void func_userprofile_show(){ string type = "linkuser"; databaselink databaselink = new databaselink(this); databaselink.execute(type); context_userprofile = getapplicationcontext(); textview = (textview)findviewbyid(r.id.tvprofilepage_user); assert textview != null; textview.settext(databaselink.thename); textview1 = (textview)findviewbyid(r.id.tvprofilepage_ign); assert textview1 != null; textview1.settext(databaselink.theign); textview2 = (textview)findviewbyid(r.id.tvprofilepage_emailid); assert textview2 != null; textview2.settext(databaselink.theemail); } }
now method diffrent class extends asynctask
, "theign" showing in alertdialog
not in textview
.
public void decodejson() { try { jsonobject jsonobj = new jsonobject(myjson); details_json = jsonobj.getjsonarray(tag_results); (int = 0; < details_json.length(); i++) { jsonobject c = details_json.getjsonobject(i); thename = c.getstring(tag_name); theign= c.getstring(tag_ign); theemail = c.getstring(tag_email); } } catch (jsonexception e) { e.printstacktrace(); } } @override protected void onpreexecute() { alertdialog = new alertdialog.builder(context).create(); alertdialog.settitle("user profile loading..."); } @override protected void onpostexecute(string result) { myjson = result; decodejson(); alertdialog.setmessage(theign); alertdialog.show(); }
the problem textview.settext called before asynctask has finished.
to solve problem can implement interface call function inside activity when info ready. assuming asynktask inside databaselink class, sow pseudocode..
interface:
public interface databaselinkinterface { public void databaselinkfinished(); }
the context listener. have context in databaselink class:
@override protected void onpostexecute(string result) { myjson = result; decodejson(); alertdialog.setmessage(theign); alertdialog.show(); databaselinkinterface mlistener = (databaselinkinterface) context; mlistener.databaselinkfinished(); }
implement interface in activity , create databaselink in oncreate:
public class userprofile extends appcompatactivity implements databaselinkinterface { textview textview , textview1 , textview2 ; context context_userprofile; databaselink databaselink; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_userprofile); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); string type = "linkuser"; databaselink = new databaselink(this); databaselink.execute(type); } ...
and finally, in activity, implements interface method know when data fetched. remove func_userprofile_show(). avoid use static variables purpose.
@override public void databaselinkfinished(){ context_userprofile = getapplicationcontext(); textview = (textview)findviewbyid(r.id.tvprofilepage_user); assert textview != null; textview.settext(databaselink.thename); textview1 = (textview)findviewbyid(r.id.tvprofilepage_ign); assert textview1 != null; textview1.settext(databaselink.theign); textview2 = (textview)findviewbyid(r.id.tvprofilepage_emailid); assert textview2 != null; textview2.settext(databaselink.theemail); }
i recommend use getters , setters instead of access directly attribute (databaselink.theemail -> databaselink.gettheemail())
Comments
Post a Comment