共享首选项读取值返回defaultvalues

时间:2013-07-22 11:09:35

标签: android sharedpreferences

我认为我在从SharedPreferences检索值时遗漏了一些东西。

我的代码:

public class SecondActivity extends Activity {

    private TextView password;
    private EditText username;
    private Button button;
    private String name;
    private String pass;
    private Set<String> set;
    private TextView show;
    SharedPreferences myPrefs;
    SharedPreferences.Editor editor;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

        myPrefs = this.getPreferences(MODE_PRIVATE);
        editor = myPrefs.edit();

        username = (EditText) findViewById(R.id.editText1);
        password = (EditText) findViewById(R.id.editText2);
        button = (Button) findViewById(R.id.button1);
        show = (TextView) findViewById(R.id.textView3);

        name = username.getText().toString();
        pass = password.getText().toString();

        /*
         * set = new HashSet<String>(); set.add(name); set.add(pass);
         */

    }

    public void saveToPreference(View v) {

        editor.putString("UserName", name);
        editor.putString("Password", pass);
        editor.commit();

        SharedPreferences myPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        String use = myPref.getString("UserName", "Sorry");
        String pas = myPref.getString("Password", "SorryAgain");
        Toast.makeText(getApplicationContext(), use, Toast.LENGTH_SHORT).show();
        show.append(use + "\n" + pas);

    }

}

App不强制/关闭。相反,我得到defaultValues; “抱歉”,“SorryAgain”从SharedPreferences检索到。 "Sorry"被烤了。

我还应该做些什么?

2 个答案:

答案 0 :(得分:3)

您正在修改返回的preferences

this.getPreferences(MODE_PRIVATE);

然后您正在阅读返回的首选项:

PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

您应该在onCreate中初始化myPref,然后在所有Activity函数中使用该实例,以确保您使用完全相同的SharedPreferences。像这样:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);

     myPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
     editor = myPrefs.edit();


     username = (EditText)findViewById(R.id.editText1);
     password = (EditText)findViewById(R.id.editText2);
     button = (Button)findViewById(R.id.button1);
     show = (TextView)findViewById(R.id.textView3);

}   


    public void saveToPreference(View v){

    editor.putString("UserName",username.getText().toString());
    editor.putString("Password", password.getText().toString());
    editor.commit();

    String use = myPrefs.getString("UserName", "Sorry");
    String pas = myPrefs.getString("Password", "SorryAgain");
    Toast.makeText(getApplicationContext(), use,Toast.LENGTH_SHORT).show();
    show.append(use+"\n"+ pas);


}

答案 1 :(得分:1)

//In oncreate if its an activity or do it on top of class
    SharedPreferences myPref= getPreferences(MODE_PRIVATE); 
     SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();

//Add data after getting it from edittext
    editor.putString("UserName",name);
     editor.putString("Password", pass);
     editor.commit();

//get data
     String use = myPref.getString("UserName", "Sorry");
     String pas = myPref.getString("Password", "SorryAgain");