android - How to return an object to the fragment from a nested static asynctask? -
i have fragment contains static asynctask class json data retrieving , parsing - after add data object workerobj
nested parcelable class in fragment. object created inside fragment , want update data after asynctask finishes error arrises when try assign new value workerobj
inside protected void onpostexecute(jsonobject jsonobject)
method - how circumvent - while keeping asynctask static (it must):
cannot make static reference non-static field workerobj
when try in onpostexecute()
workerobj = workerobject;
summarized code:
public class workerfragment extends sherlockfragment implements onclicklistener{ databasehandler db; workerparcel workerobj; ...... ..... static class workerparcel implements parcelable { ..... } private static class updateworkerasynctask extends customasynctask<workerparcel, integer, jsonobject> { private static final string tag = "dobackgroundtask"; private progressdialog mprogress; private int mcurrprogress; private view v; functions workerfunction; databasehandler db; workerparcel workerobject; public updateworkerasynctask(mainactivity activity, view rootview) { super(activity); this.v = rootview; } @override protected void onpreexecute() { super.onpreexecute(); showprogressdialog(); } @override protected void onactivitydetached() { if (mprogress != null) { mprogress.dismiss(); mprogress = null; } } @override protected void onactivityattached() { showprogressdialog(); } private void showprogressdialog() { mprogress = new progressdialog(mactivity); mprogress.setprogressstyle(progressdialog.style_spinner); mprogress.setindeterminate(true); mprogress.setmessage(" saljem na server... "); mprogress.setcancelable(true); mprogress.setoncancellistener(new oncancellistener() { @override public void oncancel(dialoginterface dialog) { cancel(true); } }); mprogress.show(); mprogress.setprogress(mcurrprogress); } @override protected jsonobject doinbackground(workerparcel... params) { workerobject = params[0]; jsonobject json = null; // need either pass instance of outer class // inner class method (or constructor) parameter, // or create inside method. if (mactivity != null) { // workerid logged worker workerfunction = new functions(); db = new databasehandler(mactivity); if(workerobject.fname.trim().equals(db.retrieveworker().get("fname"))){ workerobject.fname = null; } if(workerobject.lname.trim().equals(db.retrieveworker().get("lname"))){ workerobject.lname = null; } if(workerobject.uname.trim().equals(db.retrieveworker().get("username"))){ workerobject.uname = null; } if(workerobject.pass.trim().equals(db.retrieveworker().get("pass"))){ workerobject.pass = null; } // jsonobject functions.java json = workerfunction.updateworker(workerobject.workerid, workerobject.fname, workerobject.lname, workerobject.uname, workerobject.pass); } return json; } @override protected void onpostexecute(jsonobject jsonobject) { super.onpostexecute(jsonobject); if (mactivity != null) { mprogress.dismiss(); try { textview firstname = (textview) v.findviewbyid(r.id.fnametxt); textview lastname = (textview) v.findviewbyid(r.id.lnametxt); textview username = (textview) v.findviewbyid(r.id.unametxt); textview password = (textview) v.findviewbyid(r.id.passwordtxt); textview lastlogin = (textview) v.findviewbyid(r.id.lastlogintxt); textview errormsg = (textview) v.findviewbyid(r.id.errortxt); if (jsonobject.getstring(key_success) != null) { errormsg.settext(""); string res = jsonobject.getstring(key_success); if (integer.parseint(res) == 1) { jsonobject json_worker = jsonobject.getjsonobject("worker"); // clear previous data in database workerfunction.logoutworker(mactivity); //add worker sqllite db table //addworker(string workerid, string fname, string lname, string username, string pass, string lastlog) db.addworker(json_worker.getstring(w_id), json_worker.getstring(w_fname), json_worker.getstring(w_lname), json_worker.getstring(w_usern), json_worker.getstring(w_pass), json_worker.getstring(w_lastlogin)); //retrieve worker acount local sqllite deb hashmap<string,string> workerdb = db.retrieveworker(); //add worker workerobject workerobject = new workerparcel(workerdb.get("workerid"),workerdb.get("fname"),workerdb.get("lname"),workerdb.get("username") ,workerdb.get("pass"),workerdb.get("lastlog")); firstname.settext(workerobject.fname); lastname.settext(workerobject.lname); username.settext(workerobject.uname); password.settext(workerobject.pass); lastlogin.settext(workerobject.lastlog); workerobj = workerobject; } else { // error in updating errormsg.settext("error occured in updating account"); }
declare interface
public interface getobject { void ongetobject(workerparcel workerobj); }
force aysnctask implement , use callback retrieve results
private static class updateworkerasynctask extends customasynctask<workerparcel, integer, jsonobject> { getobject mlistener; public updateworkerasynctask(mainactivity activity, view rootview, getobject listener) { mlistener = listener; // code } @override protected void onpostexecute(jsonobject jsonobject) { if (mlistener != null) { mlistener.ongetobject(workerobject); } } }
Comments
Post a Comment