在另一个活动中加载SharedPreferences

时间:2017-02-25 12:12:46

标签: android sharedpreferences spinner

这就是问题所在。在我的第二节课中,我试图加载SharedPreferences。下面我还要包括我的第一堂课。

//set label for journal questions
public TextView journalQuestionLabel;
public int counter = 0;
SharedPreferences preferences;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_journal);

    //TODO: Send saved preferences here
    preferences = getSharedPreferences("grammarOption", MODE_PRIVATE);
    int selection = preferences.getInt("grammarOption", -1);
    Log.d("in onCreate", "preferences = " + selection);

} 

当我测试它时,我的调试日志总是打印-1。它不会加载我的共享首选项。我究竟做错了什么?

我已经在这里和每个教程中尝试了其他答案,但它们都没有用。这是我设置和保存微调器首选项的代码。我检查了这个并且它正在工作。

    private void setupSpinner() {
    // Create adapter for spinner. The list options are from the String array it will use
    // the spinner will use the default layout
    final ArrayAdapter grammarSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.array_grammar_options,
            android.R.layout.simple_spinner_dropdown_item);

    // Specify dropdown layout style - simple list view with 1 item per line
    grammarSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);

    //Apply the adapter to the spinner
    grammarChoiceSpinner.setAdapter(grammarSpinnerAdapter);

    //Create shared preferences to store the spinner selection
    SharedPreferences preferences = getApplicationContext().getSharedPreferences
            ("Selection", MODE_PRIVATE);

    editor = preferences.edit();

    // Create the intent to save the position
    grammarChoiceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            //receive the string of the option and store it
            int grammarOptionPosition = grammarChoiceSpinner.getSelectedItemPosition();
            //put the string in the editor
            editor.putInt("grammarOption", grammarOptionPosition);
            editor.commit();
            //make a toast so the user knows if it's not "select"
            if (grammarOptionPosition != 0) {
                Toast.makeText(getApplicationContext(), "Choice saved.",
                        Toast.LENGTH_SHORT).show();
            }
        }

        // Because AdapterView is an abstract class, onNothingSelected must be defined
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            mGrammar = 0;
        }
    });
}

这里是在onCreate()

中调用的
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_opening);

    //find the spinner to read user input
    grammarChoiceSpinner = (Spinner) findViewById(R.id.spinner);

    setupSpinner();

2 个答案:

答案 0 :(得分:0)

您在getSharedPreferences(String, int)中传递了错误的字符串。

preferences = getSharedPreferences("Selection", MODE_PRIVATE);
int selection = preferences.getInt("grammarOption", -1);
Log.d("in onCreate", "preferences = " + selection);

试一试。

我希望它有所帮助。

答案 1 :(得分:0)

确保您的SharedPreference密钥在课堂上或整个App中都相同 虽然保存也喜欢 -

SharedPreferences preferences = getSharedPreferences("MY_PREFS", MODE_PRIVATE);
preferences.edit().putInt("grammerOption", 1).apply();


从prefs获取数据的确如此 -

SharedPreferences preferences = getSharedPreferences("MY_PREFS", MODE_PRIVATE);
int option = preference.getInt("grammerOption", -1);


PS, 首选项的密钥(此处为MY_PREFS)必须相同。