2014-03-03 63 views
0

编辑:它由用户Guardanis在其他地方的评论解决。 Android SharedPreferences not loading and saving properlyAndroid sharedPreferences没有正确保存或获取字符串

问题是我从来没有保存它。我有一个保存功能,但从未将它保存到首选项。

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

我的程序本质上是一个字符串创建应用程序。当你按下按钮时,它会创建一个字符串作为短信发送。我的设置活动页面有四个编辑文本,通过按钮单击它们可以保存内容,并返回到主要活动。最后一个按钮通过从keyvalue对获取值并构造消息来创建一个String。但是,我对每个值始终为空。

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

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

相反Editor'的','尝试SharedPreferences.Editor' – Kedarnath

+0

立即更改此...让我们看看。没有。改变它什么都没做。 – jakeinmn

+0

你在哪里调用了这个'save(View view)'方法?或把你的所有代码? –

回答

1

改变这种

sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 

sp = getPreferences(0); 
+0

让我试试这一个,我改变了这两个activity中的两个onCreate方法中的两条线,可悲的是,它没有改变任何东西。 – jakeinmn

+0

@jakeinmn最好先发布你的所有代码? –

+0

发送一个包含项目文件链接的pastebin以及发布在我的收件箱中的整个项目本身:http://pastebin.com/KMfZKSqf – jakeinmn

1

尝试。

//declareation 
    private SharedPreferences _sPrefs=null; 
    //do this in oncreate 
    _sPrefs = getSharedPreferences("myPrefs", Context.MODE_PRIVATE); 
    //this is for your editor 
    SharedPreferences.Editor prefEditor = _sPrefs.edit(); 
1

SharedPreferences类提供了一个总体框架,使您可以保存和检索的原始数据类型的持久键 - 值对。您可以使用SharedPreferences来保存任何原始数据:布尔值,浮点数,整数,长整数和字符串。

您可以将四种文本保存为:

SharedPreferences shared=getSharedPreferences("app_name", Activity.MODE_PRIVATE); 
shared.edit().putString("text_name1", "value1").commit(); 

然后在活动(你需要让他们回来)

SharedPreferences shared=getSharedPreferences("app_name", Activity.MODE_PRIVATE); 
String text_name1=shared.getString("text_name1", null);