java - Modifying data from an Async task in an entirely different class -
i know out of curiosity if there convenient ways of pulling data out of async task created inside class, , modifying data in class (without extending classes)
i have way it, involves making methods static along async task itself
for example, here i'm making string "text" in async task
public class main extends activity{ //context ctx; static class myasynctask extends asynctask<void,string,string>{ static string result; private static context context; public myasynctask(context m) { this.context = m; } @override protected string doinbackground(void... noargs) { result = "text"; return result; } protected void onpostexecute(string result) { super.onpostexecute(result); } public static string getstr() { return result; } }; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final edittext et = (edittext)findviewbyid(r.id.edittext1); button btn = (button)findviewbyid(r.id.button1); myasynctask task = new myasynctask(this); task.execute(); final test t = new test(); btn.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { et.append(t.modifiedstring()); } }); } }
and in new class make simple string method modify data async task
public class test{ public string modifiedstring() { // main main = null; // myasynctask task = new myasynctask(main.ctx); // task.execute(); string s = (string)main.myasynctask.getstr(); return "modified " + s + "\n"; } }
i'm wondering, there way can without having make async task static? perhaps sharing contexts or something?
by way i'm not doing solve particular problem, i'm doing out of curiosity
just create singleton
public class main extends activity{ public static main instance; public static string thestring; public class myasynctask extends asynctask<void,string,string>{ static final string result = "text"; context context; public myasynctask(context m) { this.context = m; } @override protected string doinbackground(void... noargs) { return result; } protected void onpostexecute(string result) { super.onpostexecute(result); } public string getstr() { return result; } }; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final edittext et = (edittext)findviewbyid(r.id.edittext1); button btn = (button)findviewbyid(r.id.button1); myasynctask task = new myasynctask(this); task.execute(); thestring = task.getstr(); instance = this; final test t = new test(); btn.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { et.append(t.modifiedstring()); } }); } public string pulledfromasynctask() { return thestring; } public static main getinstance(){ return instance; } }
and in class
public class test{ public string modifiedstring() { main main = main.getinstance(); //so main.something.. can call methods want //a solution make singleton class myasynctask setting //functions get/set can take values other classes return "modified " + main.pulledfromasynctask() + "\n"; } }
Comments
Post a Comment