2011-06-20 154 views
3

在我的活動中,我根據存儲的偏好更新用戶界面。對於updateUI代碼如下:不共享偏好不被保存

private void updateUI() 
{ 
    //preferences = getSharedPreferences(Select.PREF_FILE_NAME, MODE_PRIVATE); 
    preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 

    toggle = (Button)findViewById(R.id.toggleButton); 
    incommingEdit = (Button)findViewById(R.id.IncommingEditButton); 
    outgoingEdit = (Button)findViewById(R.id.outgoingEditButton); 
    missedEdit = (Button)findViewById(R.id.missedEditButton); 
    save = (Button)findViewById(R.id.saveButton); 
    cancel = (Button)findViewById(R.id.cancelButton); 
    incommingCheck = (CheckBox)findViewById(R.id.incommingCheck); 
    outgoingCheck = (CheckBox)findViewById(R.id.outgoingCheck); 
    missedCheck = (CheckBox)findViewById(R.id.missedCheck); 
    incommingTextView = (TextView) findViewById(R.id.incommingTextView); 
    outgoingTextView = (TextView) findViewById(R.id.outgoingTextView); 
    missedTextView = (TextView) findViewById(R.id.missedTextView); 

    //Disable all the edit buttons until their checkboxes are checked. 
    incommingEdit.setEnabled(false); 
    outgoingEdit.setEnabled(false); 
    missedEdit.setEnabled(false); 

    //Display the messages in the text views. 
    incommingTextView.setText(preferences.getString("incommingMsgPhone", "Currently there are no messages saved.")); 
    outgoingTextView.setText(preferences.getString("outgoingMsgPhone", "Currently there are no messages saved.")); 
    missedTextView.setText(preferences.getString("missedMsgPhone", "Currently there are no messages saved.")); 

    //Check the check boxes. 
    if(preferences.getInt("incommingPhone", 0) == Calls.INCOMING_TYPE) 
    { 
     incommingCheck.setChecked(true); 
     incommingEdit.setEnabled(true); 
    } 

    if(preferences.getInt("outgoingPhone", 0) == Calls.OUTGOING_TYPE) 
    { 
     outgoingCheck.setChecked(true); 
     outgoingEdit.setEnabled(true); 
    } 

    if(preferences.getInt("missedPhone", 0) == Calls.MISSED_TYPE) 
    { 
     missedCheck.setChecked(true); 
     missedEdit.setEnabled(true); 
    } 

    //Check if the application is on or off and set the text of the button. 
    //preferences = getSharedPreferences(Select.PREF_FILE_NAME, MODE_PRIVATE); 
    boolean on = preferences.getBoolean("isOn", false); 
    if(!on) 
     toggle.setText("Turn On"); 
    else 
     toggle.setText("Turn off"); 
} 

這裏是我如何保存所有這些首選項:

save.setOnClickListener(new OnClickListener() 
    { 

     @Override 
     public void onClick(View v) 
     { 
      // TODO Auto-generated method stub 
      //Save all in the preference file and exit. 
      //preferences = getSharedPreferences(Select.PREF_FILE_NAME, MODE_PRIVATE); 
      Editor editor = preferences.edit(); 
      editor.putInt("incommingPhone", incomming); 
      editor.putInt("outgoingPhone", outgoing); 
      editor.putInt("missedPhone", missed); 

      editor.putString("incommingMsgPhone", incommingMsg); 
      editor.putString("outgoingMsgPhone", outgoingMsg); 
      editor.putString("missedMsgPhone", missedMsg); 

      editor.commit(); 
      finish(); 
     } 
    }); 

我的UI更新正確的第二次,我跑我的應用程序,但圍繞第三次或第四次我得到默認首選項值。我甚至嘗試使用getdefaultpreferences而不是getsharedpreferences,但沒有運氣。

回答

0

試試這個..

preferences = activity.getSharedPreferences("Share", Context.MODE_PRIVATE);` 
+0

謝謝,但我也試過這種方法,但不知何故,我的喜好被刪除。 – coders1290

-1

它的例子:

public void saveFavName(String what) 
     { 


      myPrefs= getSharedPreferences("myPrefs", MODE_PRIVATE); 
      SharedPreferences.Editor es = myPrefs.edit(); 
      es.putString("value", what); // add or overwrite someValue 
       es.commit(); // this saves to disk and notifies observers 
     } 
1
我有一個類似的問題

- 我的字符串變量被保留,但沒有別的。 作爲一個窮人的解決方法,我將所有東西都保存爲字符串。

2

嘗試在使用前清除編輯器。換句話說:

Editor editor = preferences.edit(); 
editor.clear(); 
editor.putInt("incommingPhone", incomming); 
editor.commit(); 

我有和你一樣的問題,這對我有效。對於我的整個代碼示例見this post on my blog

5

我有getStringSet類似的問題,documatation幫助有

請注意,您不能修改此調用返回集合實例。 如果存儲數據的一致性不能保證,也不能保證您完全可以修改該實例。

+0

謝謝你!保存了我的一天。 – Auriukas