开发者

How to use SharedPreferences [duplicate]

This question already has answers here: Using shared preferences editor (3 answers) Closed 9 years ago.

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 u开发者_如何学Pythonse, 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();
    }
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜