SharedPreferences不保存,只有默认值

时间:2014-11-23 19:20:16

标签: sharedpreferences

我在android studio中编写了一个Android应用程序。我的ProfileView类扩展了activity_profile_view.xml活动,允许用户更改其名称和描述。但每次调用onCreate或onResume方法时,都会显示默认值。没有显示应该在onClick或onPause中保存的值。谢谢,这是我的第一篇文章。

public class ProfileView extends Activity implements View.OnClickListener{

public EditText NameEdit, AboutEdit;
public Button SaveButton;
public ImageButton NewPic;
public static final String Profile_Prefs = "Pro_File";
public static SharedPreferences profile;

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_profile_view);
   NameEdit = (EditText) findViewById(R.id.NameEdit);
   AboutEdit = (EditText) findViewById(R.id.AboutEdit);
   SaveButton = (Button) findViewById(R.id.SaveButton);

   SaveButton.setOnClickListener(this);

  //Restore Preferences
   profile = this.getSharedPreferences(Profile_Prefs, 0);
   AboutEdit.setText(profile.getString("about", "About Me"));
   NameEdit.setText(profile.getString("name","name"));
}
 @Override
public void onClick(View v) {

            SharedPreferences profile = getSharedPreferences(Profile_Prefs, 0);
            SharedPreferences.Editor editor = profile.edit();
            editor.putString(NameEdit.toString(),"name");
            editor.putString(AboutEdit.toString(),"about");
            editor.apply();
            Intent mainIntent = new Intent(this, Main.class);
            startActivity(mainIntent);

};
@Override
protected void onPause(){
    super.onPause();
    SharedPreferences profile = getSharedPreferences(Profile_Prefs, 0);
    SharedPreferences.Editor editor = profile.edit();
    editor.putString(NameEdit.toString(),"name");
    editor.putString(AboutEdit.toString(),"about");
    editor.commit();
}
@Override()
protected void onResume(){
    super.onResume();
    profile = this.getSharedPreferences(Profile_Prefs, 0);
    AboutEdit.setText(profile.getString("about", "About Me"));
    NameEdit.setText(profile.getString("name","name"));
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.profile_view, menu);
    return true;
}

修复onClick和onPause后,当调用onResume或onCreate时,我会在app中的editText字段中进行操作。

  

android.widget.EditText {1a0ea60cVFED..CL.F ...... 32,263736,426#7f0a0003 app:id / NameEdit}   android.widget.EditText {651555eVFED..CL ........ 92456-676,796#7f0a0009 app:id / AboutEdit}

1 个答案:

答案 0 :(得分:0)

我认为在OnClick()方法中你需要改变这个:

@Override
public void onClick(View v) {

        SharedPreferences profile = getSharedPreferences(Profile_Prefs, 0);
        SharedPreferences.Editor editor = profile.edit();
        editor.putString(NameEdit.toString(),"name");
        editor.putString(AboutEdit.toString(),"about");
        editor.apply();
        Intent mainIntent = new Intent(this, Main.class);
        startActivity(mainIntent);

};

用这个:

@Override
public void onClick(View v) {

        SharedPreferences profile = getSharedPreferences(Profile_Prefs, 0);
        SharedPreferences.Editor editor = profile.edit();
        editor.putString("name",NameEdit.getText().toString()); //HERE I THINK YOU INVERTED KEY,VALUE ORDER, THIS IS THE CORRECT WAY
        editor.putString("about",AboutEdit.getText().toString()); //INVERTED KEY,VALUE ORDER HERE TOO
        editor.apply();
        Intent mainIntent = new Intent(this, Main.class);
        startActivity(mainIntent);
};

希望这有帮助

编辑:我没注意到你在onPause()方法中有相同的内容:

@Override
protected void onPause(){
    super.onPause();
    SharedPreferences profile = getSharedPreferences(Profile_Prefs, 0);
    SharedPreferences.Editor editor = profile.edit();
    editor.putString("name",NameEdit.getText().toString()); //I THINK THIS IS THE CORRECT WAY
    editor.putString("about",AboutEdit.getText().toString()); //I THINK THIS IS THE CORRECT WAY
    editor.commit();
}