I'm new to android development - using a book called Sams Teach Yourself Android Application Development in 24 hours. Followed it so far but got stuck trying to use SharedPreferences.
In the folder src/com.androidbook.triviaquiz I've got a file called QuizActivity, in it I've got the following:
package com.androidbook.triviaquiz;
import android.app.Activity;
import android.os.Bundle;
import android.content.SharedPreferences;
public class QuizActivity extends Activity {
public static final String GAME_PREFERENCES = "GamePrefs";
SharedPreferences settings = getSharedPreferences(GAME_PREFERENCES, MODE_PRIVATE);
SharedPreferences.Editor prefEditor = settings.edit();
prefEditor.putString("UserName", "JaneDoe");
prefEditor.putInt("UserAge", 22);
prefEditor.commit();
}
This is what the book tells me to use, but it returns errors at the following points: under the "." after the first 2 prefEditor statements, under ("UserName", "JaneDoe") under ("UserAge", 22); and under "commit"
I've looked on loads of websites for help but all seem to use the same code. What am I doing wrong?
Try putting it in your onCreate(). Also, getDefaultSharedPreferences()
is easier:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Access the default SharedPreferences
SharedPreferences preferences =
PreferenceManager.getDefaultSharedPreferences(this);
// The SharedPreferences editor - must use commit() to submit changes
SharedPreferences.Editor editor = preferences.edit();
// Edit the saved preferences
editor.putString("UserName", "JaneDoe");
editor.putInt("UserAge", 22);
editor.commit();
}
Use SharedPreferences only after the activity is created. Initialize SharedPreferences in onCreate().
public class QuizActivity extends Activity {
public static final String GAME_PREFERENCES = "GamePrefs";
SharedPreferences settings;
SharedPreferences.Editor prefEditor;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
settings = getSharedPreferences(GAME_PREFERENCES, MODE_PRIVATE);
prefEditor = settings.edit();
prefEditor.putString("UserName", "JaneDoe");
prefEditor.putInt("UserAge", 22);
prefEditor.commit();
}
}
SharedPreferences is one of the StorageOptions in android which is used to store user preferences.This is accessible throughout the application.Example of SharedPreferences. http://www.codestacks.in/2013/03/sharedpreferences/
Example code:
package com.example.test;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Toast;
public class SharedPreferencesDemo extends Activity {
SharedPreferences shared_preferences;
SharedPreferences.Editor shared_preferences_editor;
String test_string = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
shared_preferences = getSharedPreferences("shared_preferences_test",
MODE_PRIVATE);
test_string = shared_preferences.getString("test_key", "Default");
Toast.makeText(getApplicationContext(), test_string, Toast.LENGTH_SHORT)
.show();
shared_preferences_editor = shared_preferences.edit();
shared_preferences_editor.putString("test_key", "Hello World");
shared_preferences_editor.commit();
test_string=shared_preferences.getString("test_key", "Default");
Toast.makeText(getApplicationContext(), test_string, Toast.LENGTH_SHORT).show();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With