Reputation: 307
I am trying to learn sharedPreferences in android and have written small piece of code for the same. But once i execute it, The application does not show any output.
Here is my 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 is my mainactivity that is making use of this class:
public class MainActivity extends Activity implements View.OnClickListener{
ImageButton bLogOut,bHowItWorks;
TextView SchoolDetail,AllClassDetail,TotalStudents,OneStudentDetail;
//this is for having access to userlocalstore to save/remove local data to user phone during login or Log out. So that if logout is pressed
//the data is wiped off the local storage on user mobile
UserLocalStore userLocalStore;
//Added to get the data from 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 for QR codes scanner
bHowItWorks = (ImageButton) findViewById(R.id.bHowItWorks);
bHowItWorks.setOnClickListener(this);
SchoolDetail = (TextView)findViewById(R.id.SchoolDetail);
//Added to get the data from 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 is my 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 the execution, I am not getting any data. Please let me know if I am missing something
Upvotes: 0
Views: 33
Reputation: 5940
check your code of SharedPreferencesExecutor
public String getValue(Context context,String key) {
SharedPreferences settings;
String text;
//settings = PreferenceManager.getDefaultSharedPreferences(context);
// here you are passing 'key' by mistake change it to 'PREFS_NAME'
settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
text = settings.getString(key, null);
return text;
}
surely it'll work..
Upvotes: 1