2014-03-03 88 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);