Android SharedPreferences未正确加载和保存

时间:2014-03-03 18:47:18

标签: android sharedpreferences

我从保存的偏好设置中获取字符串时返回null。我不确定保存的偏好是如何工作的,但我的理解是,当调用共享偏好时,它会在手机上创建密钥对文件,以便您稍后再回来。

我的程序本质上是一个字符串创建应用程序。当您按下按钮时,它会创建一个要作为短信发送的字符串。我的设置活动页面有四个edittexts,通过buttonclick保存其中的任何内容并返回主活动。最后一个按钮通过从keyvalue对获取值并构造消息来创建String。但是,我总是为每个值得到null。

继承设置页面的代码,然后是主页面。请问我是否可以添加更多,我没有添加所有代码,只是共享偏好部分。

public SharedPreferences sp;
public Editor e;

public void savethethings(){ //run this when enter is pressed/savew
    EditText smsintro_hint = (EditText) findViewById(R.id.settings_smsintro_hint);
    EditText smsbody_hint = (EditText) findViewById(R.id.settings_smsbody_hint);
    EditText checkboxbody_hint = (EditText) findViewById(R.id.settings_checkboxbody_hint);
    EditText checkboxbody_description_hint = (EditText) findViewById(R.id.settings_checkboxbody_description_hint);


    String introstring = smsintro_hint.getText().toString();
    String bodystring = smsbody_hint.getText().toString();
  String checkboxbodystring = checkboxbody_hint.getText().toString();
  String checkboxdescriptionstring = checkboxbody_description_hint.getText().toString();


      e.putString("intro", introstring);
      e.commit(); // you forgot to commit


  if(!bodystring.isEmpty())
  {
      e.putString("body", bodystring);
      e.commit(); 
  }

  if(!checkboxbodystring.isEmpty())
  {
      e.putString("checkbody", checkboxbodystring);
      e.commit(); 
  }

  if(!checkboxdescriptionstring.isEmpty())
  {
      e.putString("checkboxdescr", checkboxdescriptionstring);
      e.commit(); 
  }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.settingmenu);

    //SP
    sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); // forget about
    // named preferences - get the default ones and finish with it
    e = sp.edit();

    Button tt = (Button)findViewById(R.id.savebutton);
    tt.setOnClickListener(new View.OnClickListener() { 
        @Override
        public void onClick(View v) {
                finish();

        }
    });


public void save(View view)
{
    //THINGS HAPPEN HERE WITH SHARED PREFERENCES :(
    savethethings();

    this.finish();
    return;
}

public String finishedtext(String userstring)
{

    smsintroduction = (sp.getString("intro", ""));
    smsbody = (sp.getString("body", ""));
    checkboxtext = (sp.getString("checkbody", ""));
    checkboxmessage = (sp.getString("checkboxdescr", ""));

    if(smsintroduction.isEmpty()) 
    {
        if(smsbody.isEmpty())
        {
            if(checkboxtext.isEmpty())

            {
                if(checkboxmessage.isEmpty()) //topkek for most AND statements Ive ever put in in if/then form
                {
                    //Essentially the DEFAULT if they're ALL null
                    smsbody = "Hi "+ userstring +"! This is coming from jake's phone and it wants to send a text so we can talk or whatever. ";
                }
            }
        }
    }




    Toast.makeText( this, "Creating text, then press send!", Toast.LENGTH_LONG).show();

    String thetext = "";

    thetext = smsintroduction + " " + smsbody + " " + checkboxtext;

    return thetext;
}

public void savethethings(){ //run this when enter is pressed/savew
    EditText smsintro_hint = (EditText) findViewById(R.id.settings_smsintro_hint);
    EditText smsbody_hint = (EditText) findViewById(R.id.settings_smsbody_hint);
    EditText checkboxbody_hint = (EditText) findViewById(R.id.settings_checkboxbody_hint);
    EditText checkboxbody_description_hint = (EditText) findViewById(R.id.settings_checkboxbody_description_hint);


    String introstring = smsintro_hint.getText().toString();
    String bodystring = smsbody_hint.getText().toString();
  String checkboxbodystring = checkboxbody_hint.getText().toString();
  String checkboxdescriptionstring = checkboxbody_description_hint.getText().toString();

  //      if(!introstring.isEmpty()) //if the fields are NOT empty, they should get saved.
  //      {
      e.putString("intro", introstring);
      e.commit(); // you forgot to commit


  if(!bodystring.isEmpty())
  {
      e.putString("body", bodystring);
      e.commit(); 
  }

  if(!checkboxbodystring.isEmpty())
  {
      e.putString("checkbody", checkboxbodystring);
      e.commit(); 
  }

  if(!checkboxdescriptionstring.isEmpty())
  {
      e.putString("checkboxdescr", checkboxdescriptionstring);
      e.commit(); 
  }
}

1 个答案:

答案 0 :(得分:0)

创建名为SessionManger的java类,并设置所有方法来设置和获取SharedPreferences值。如果要保存值,请使用此类的对象并设置并获取值。  示例代码如下。

public class SessionManager {         
SharedPreferences pref;
SharedPreferences.Editor editor;
Context _context;    
int PRIVATE_MODE = 0;

public SessionManager(Context context) {
    this._context = context;
    pref = _context.getSharedPreferences("name_that_you_use", PRIVATE_MODE);
    editor = pref.edit();
    editor.apply();
}

public void setIntroMessage(String data) {
    editor.putString("intro", data);
    editor.commit();        
}   

public String getIntroMessage() {
    return pref.getString("intro", null);
} 

}