Android SharedPreferences 未正确加载和保存 [英] Android SharedPreferences not loading and saving properly

查看:30
本文介绍了Android SharedPreferences 未正确加载和保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我保存的首选项中获取字符串,我一直得到空返回.我不确定savedpreferences 是如何工作的,但我的理解是,当调用sharedpreferences 时,它会在手机上创建密钥对文件,以便您稍后返回.

I've been getting null returns from getting strings from my saved preferences. I'm not sure how savedpreferences worked but my understanding was that when call a sharedpreferences, it creates the keypair file on the phone so you can come back to it later.

我的程序本质上是一个字符串创建应用程序.当您按下按钮时,它会创建一个字符串作为短信发送.我的设置活动页面有四个编辑文本,通过单击按钮保存其中的任何内容并返回主活动.最后一个按钮通过从键值对中获取值来创建一个字符串并构造消息.但是,对于每个值,我总是为空.

My program is essentially a string creation application. When you press a button, it creates a string to send as an sms. My settings activity page has four edittexts that save whatever is inside them with a buttonclick and returns to the main activity. The final button creates a String by getting the value from the keyvalue pair and constructs the message. However, I've always gotten null for each of the values.

这是设置页面的代码,然后是主页.请问我是否可以添加更多,我没有添加所有代码,只添加了 sharedpreferences 部分.

Heres the code for the settings page and then the main page. Please ask if I could add more, I didn't add ALL of the code, just the sharedpreferences portions.

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(); 
  }
}

推荐答案

创建名为 SessionManger 的 java 类,并放置所有用于设置和获取 SharedPreferences 值的方法.当您想保存值时,请使用此类的对象并设置和获取值.下面给出了示例代码.

Create java class named SessionManger and put all your methods for setting and getting SharedPreferences values. When you want to save value use the object of this class and set and get the values. Sample code given below.

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);
} 

}

这篇关于Android SharedPreferences 未正确加载和保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆