2011-06-20 12 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が更新され、適切に二度目デフォルトの優先順位値。私はgetsharedpreferencesの代わりにgetdefaultpreferencesを使ってみましたが、運はありません。私の文字列変数が保持されているが、他には何もありません - 私は同様の問題を抱えてい

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 
     } 

答えて

0

は、それは一例です。..

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

ありがとうございましたが、この方法も試しましたが、何とか自分の設定が削除されています。 – coders1290

-1

これを試してみてください。 貧しい人の回避策として、私はすべてをストリングとして保存しています。

1

2

エディタを使用する前に消去してください。言い換えれば、:

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

私はあなたのように全く同じ問題を抱えていましたが、これは私のために働いています。私の全体のコードサンプルはthis post on my blog

5

を見るために私はgetStringSetと同様の問題があった、documatationは、あなたがこの呼び出しによって返されたセットのインスタンスを変更してはならないこと

注意が助けました。 保存したデータの一貫性は保証されません。また、 の場合、インスタンスをまったく変更できません。

+0

ありがとうございました!私の日を救った。 – Auriukas

関連する問題