Getting no output while using getPreference in Android -
i trying learn sharedpreferences in android , have written small piece of code same. once execute it, application not show output.
here preference class:
public class sharedpreferncesexecutor { public static final string prefs_name = "aop_prefs"; public static final string prefs_key = "aop_prefs_string"; public sharedpreferncesexecutor() { super(); } public void save(context context, string key, string value) { sharedpreferences settings; sharedpreferences.editor editor; //settings = preferencemanager.getdefaultsharedpreferences(context); settings = context.getsharedpreferences(prefs_name, context.mode_private); //1 editor = settings.edit(); //2 editor.putstring(key, value); //3 editor.commit(); //4 } public string getvalue(context context,string key) { sharedpreferences settings; string text; //settings = preferencemanager.getdefaultsharedpreferences(context); settings = context.getsharedpreferences(key, context.mode_private); text = settings.getstring(key, null); return text; } public void clearsharedpreference(context context) { sharedpreferences settings; sharedpreferences.editor editor; //settings = preferencemanager.getdefaultsharedpreferences(context); settings = context.getsharedpreferences(prefs_name, context.mode_private); editor = settings.edit(); editor.clear(); editor.commit(); } public void removevalue(context context) { sharedpreferences settings; sharedpreferences.editor editor; settings = context.getsharedpreferences(prefs_name, context.mode_private); editor = settings.edit(); editor.remove(prefs_key); editor.commit(); } } and here mainactivity making use of class:
public class mainactivity extends activity implements view.onclicklistener{ imagebutton blogout,bhowitworks; textview schooldetail,allclassdetail,totalstudents,onestudentdetail; //this having access userlocalstore save/remove local data user phone during login or log out. if logout pressed //the data wiped off local storage on user mobile userlocalstore userlocalstore; //added data sharedpreferencesexecutor private sharedpreferncesexecutor sharedpreference; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, //windowmanager.layoutparams.flag_fullscreen); setcontentview(r.layout.activity_main); //etemail= (edittext)findviewbyid(r.id.etemail); //etphonenumber =(edittext)findviewbyid(r.id.etphonenumber); blogout = (imagebutton) findviewbyid(r.id.blogout); blogout.setonclicklistener(this); userlocalstore = new userlocalstore(this); //added qr codes scanner bhowitworks = (imagebutton) findviewbyid(r.id.bhowitworks); bhowitworks.setonclicklistener(this); schooldetail = (textview)findviewbyid(r.id.schooldetail); //added data sharedpreferencesexecutor sharedpreference = new sharedpreferncesexecutor(); activity context = this; sharedpreference.save(context,"schooldata","kv station;;;class1-seca;;;class1-secb;;;class1-secc;;;class4;;;class2-seca;;;class2-secb;;;class2-secc;;;class3-seca;;;class3-secb;;;class3-secc;;;class4-seca;;;class4-secb;;;class4-secc;;;class5-seca;;;class5-secb;;;class5-secc"); string schooldata = sharedpreference.getvalue(context,"schooldata"); schooldetail.settext(schooldata); } @override public void onclick(view v) { switch(v.getid()){ case r.id.blogout: userlocalstore.clearuserdata(); userlocalstore.setuserloggedin(false); startactivity(new intent(this,login.class)); break; case r.id.bhowitworks: startactivity(new intent(this,howitworks.class)); break; } } @override protected void onstart() { super.onstart(); if (authenticate() == true) { displayuserdetails(); } else{ intent intentlogin = new intent(mainactivity.this,login.class); intentlogin.setflags(intent.flag_activity_clear_task | intent.flag_activity_new_task); startactivity(intentlogin); } } public boolean authenticate(){ return userlocalstore.getuserloggedin(); } public void displayuserdetails(){ //user user = userlocalstore.getloggedinuser(); //etemail.settext(user.email); //etphonenumber.settext(user.phonenumber + ""); } } here activitymain.xml:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@drawable/bluebackgroundlowbits1" android:weightsum="8" android:id="@+id/abc" tools:context="com.example.amandeepsingh.loginregisterandother.mainactivity"> <scrollview android:id="@+id/scrollview" android:layout_width="match_parent" android:layout_height="match_parent" android:fillviewport="true" > <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <linearlayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="0.5" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textcolor="#ffffff" android:textsize="20dp" android:id="@+id/schooldetail"/> </linearlayout> <linearlayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textcolor="#ffffff" android:textsize="20dp" android:id="@+id/allclassdetail"/> </linearlayout> <linearlayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:weightsum="3" android:background="@drawable/bluebackgroundlowbits1"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textcolor="#ffffff" android:textsize="20dp" android:id="@+id/totalstudents"/> </linearlayout> <linearlayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:weightsum="3" android:background="@drawable/bluebackgroundlowbits1"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textcolor="#ffffff" android:textsize="20dp" android:id="@+id/onestudentdetail"/> </linearlayout> <linearlayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:weightsum="1" android:background="@drawable/bluebackgroundlowbits1"> <imagebutton android:id="@+id/blogout" android:text="logout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginbottom="10dp" android:textsize="20dp"/> <imagebutton android:id="@+id/bhowitworks" android:text="logout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginbottom="10dp" android:textsize="20dp"/> </linearlayout> </linearlayout> </scrollview> </linearlayout> after execution, not getting data. please let me know if missing something
check code of sharedpreferencesexecutor
public string getvalue(context context,string key) { sharedpreferences settings; string text; //settings = preferencemanager.getdefaultsharedpreferences(context); // here passing 'key' mistake change 'prefs_name' settings = context.getsharedpreferences(prefs_name, context.mode_private); text = settings.getstring(key, null); return text; } surely it'll work..
Comments
Post a Comment