共享首选项未设置

时间:2012-03-16 05:28:12

标签: java android

我在活动开始时使用以下代码

 ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, transferToOrFromArray);
         spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
         transferTo.setAdapter(spinnerArrayAdapter);
         transferFrom.setAdapter(spinnerArrayAdapter);

         SharedPreferences CNPASettings = getSharedPreferences("CNPA",0);
         transferTo.setSelection(spinnerArrayAdapter.getPosition(CNPASettings.getString("transferTo",spinnerArrayAdapter.getItem(0))));
         transferFrom.setSelection(spinnerArrayAdapter.getPosition(CNPASettings.getString("transferFrom",spinnerArrayAdapter.getItem(0))));

当我完成任务时,按下完成按钮,其中包含以下代码。

                        title = "Success";
                        error = "Transfer has been done";
                        SharedPreferences CNPASettings = getSharedPreferences("CNPA",0);
                        SharedPreferences.Editor prefEditor = CNPASettings.edit();  
                        prefEditor.putString("tranferFrom", transferFrom.getSelectedItem().toString());  
                        prefEditor.putString("tranferTo", transferTo.getSelectedItem().toString());  
                        prefEditor.commit();  
finish();

但是当我再次创建活动时,它会传递默认值spinnerArrayAdapter.getItem(0))

我做错了什么?

2 个答案:

答案 0 :(得分:2)

("tranferFrom", transferFrom.getSelectedItem().toString());  
                        prefEditor.putString("tranferTo"


     CNPASettings.getString("transferTo"
     CNPASettings.getString("transferFrom"

转移和转移.....拼写错误......

答案 1 :(得分:0)

用于setselection使用位置编号而不是字符串。根据该位置,我们将当前焦点设置为微调器。 参考检查以下链接 Shared Preferences in android