web services - Passing data from Activity to Fragment in android -
i have started android , working on simple login page,where user can use email , password login conditions while login. when user login "store owner",his name , email shown on next page (i.e on fragment class) working fine whenever user login "public user",his name , email not showing on next page.
note: using webservices , sharedpreferences fetch , save data.
thanks in advance
here code
loginactivity
public class loginactivity extends appcompatactivity implements view.onclicklistener { edittext login_emailid, login_password; string str_login_email, str_login_password; button login_button; public static final string my_prefs_names = "myprefsfiles"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_login); login_button = (button) findviewbyid(r.id.loginbtn); login_emailid = (edittext) findviewbyid(r.id.login_emailid); login_password = (edittext) findviewbyid(r.id.login_password); login_button = (button) findviewbyid(r.id.loginbtn); login_button.setonclicklistener(this); } @override public void onclick(view v) { if (v == login_button) { str_login_email = login_emailid.gettext().tostring(); str_login_password = login_password.gettext().tostring(); if (str_login_email.isempty()) { login_emailid.seterror("enter email"); } else if (str_login_password.isempty()) { login_password.seterror("password please"); } else { new asynctaskrunner().execute(); } } } } public static boolean isemailvalid(string email) { return !(email == null || textutils.isempty(email)) && android.util.patterns.email_address.matcher(email).matches(); } public string connection(string action) { string result = ""; httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://beta.gkninternational.life/webservice/user_login.php");// replace url httppost.addheader("content-type", "application/x-www-form-urlencoded"); list<namevaluepair> namevaluepairlist = new arraylist<namevaluepair>(); namevaluepairlist.add(new basicnamevaluepair( "action", action)); namevaluepairlist.add(new basicnamevaluepair( "email", str_login_email)); namevaluepairlist.add(new basicnamevaluepair( "password", str_login_password)); try { log.e("namevaluepairlist", " " + namevaluepairlist); urlencodedformentity urlencodedformentity = new urlencodedformentity( namevaluepairlist); httppost.setentity(urlencodedformentity); try { httpresponse httpresponse = httpclient .execute(httppost); inputstream inputstream = httpresponse.getentity() .getcontent(); inputstreamreader inputstreamreader = new inputstreamreader( inputstream); bufferedreader bufferedreader = new bufferedreader( inputstreamreader); stringbuilder stringbuilder = new stringbuilder(); string bufferedstrchunk = null; while ((bufferedstrchunk = bufferedreader.readline()) != null) { stringbuilder.append(bufferedstrchunk); } result = stringbuilder.tostring(); log.e("result", stringbuilder.tostring()); return stringbuilder.tostring(); } catch (clientprotocolexception cpe) { system.out .println("first exception coz of httpresponese :" + cpe); cpe.printstacktrace(); } catch (ioexception ioe) { system.out .println("second exception coz of httpresponse :" + ioe); ioe.printstacktrace(); } } catch (unsupportedencodingexception uee) { system.out.println("an exception given because of urlencodedformentity argument :" + uee); uee.printstacktrace(); } return result; } private class asynctaskrunner extends asynctask<string, string, string> { progressdialog mprogressbar; string resp; int success; string message; string name, email, password, userrole, userpic, iddd; string storeid,storename; sharedpreferences sharedpreferenceslogin; sharedpreferences.editor editor; @override protected string doinbackground(string... params) { string result = connection("login_activity"); try { sharedpreferenceslogin = getsharedpreferences(my_prefs_names, context.mode_private); editor = sharedpreferenceslogin.edit(); jsonobject jsonobject = new jsonobject(result); success = jsonobject.getint("success"); message = jsonobject.getstring("message"); iddd = jsonobject.getstring("userid"); name = jsonobject.getstring("firstname"); email = jsonobject.getstring("email"); userrole = jsonobject.getstring("userrole"); storeid = jsonobject.getstring("storeid"); storename = jsonobject.getstring("storename"); editor.putstring("userid", name); editor.putstring("firstname", name); editor.putstring("email", email); editor.putstring("userrole", userrole); editor.putstring("storeidlogin", storeid); editor.putstring("storenamelogin", storename); editor.apply(); } catch (exception e) { e.printstacktrace(); } return resp; } @override protected void onpostexecute(string result) { // execution of result of long time consuming operation mprogressbar.dismiss(); if (success == 2) { toast.maketext(loginactivity.this, message, toast.length_long).show(); } else if (success == 1) { toast.maketext(loginactivity.this, message, toast.length_long).show(); if(userrole.equals("store_owner")) { if(storename.equals("dummy")){ startactivity(new intent(loginactivity.this, editactivity.class)); finish(); } else{ intent intentt = new intent(loginactivity.this, homeactivity.class); startactivity(intentt); finish(); } } else{ intent intentuser = new intent(loginactivity.this, aftersplash.class); startactivity(intentuser); finish(); } } } @override protected void onpreexecute() { mprogressbar = new progressdialog(loginactivity.this); mprogressbar.setmessage("connecting server"); mprogressbar.setcancelable(false); mprogressbar.show(); } } frament class
public class myfragment extends fragment{
sharedpreferences sharedpreferenceslogin; public static final string my_prefs_names = "myprefsfiles"; textview names,emails; string str_names,str_emails; public myfragment() { // required empty public constructor } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view layout = inflater.inflate(r.layout.fragment_my, container, false); sharedpreferenceslogin= getactivity().getsharedpreferences(my_prefs_names, context.mode_private); names=(textview)layout.findviewbyid(r.id.names); emails=(textview)layout.findviewbyid(r.id.emails); str_names= sharedpreferenceslogin.getstring("firstname",""); str_emails= sharedpreferenceslogin.getstring("email",""); names.settext(str_names); emails.settext(str_emails); } }
override newinstance() method in fragment this.
public static yourfragment newinstance(bundle bundle) { activityfeedlistfragment fragment = new activityfeedlistfragment(); fragment.setarguments(bundle); return fragment; } and pass bundle object.
Comments
Post a Comment