共享偏好设置未保存

3

在我的活动中,我根据存储的偏好更新用户界面。更新UI的代码如下:

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,但没有成功。

5个回答

5

我曾经遇到过与getStringSet类似的问题,文档帮了我的忙。

请注意,您不应修改此调用返回的set实例。 如果您这样做,存储数据的一致性将无法保证,而且您也无法修改该实例。


3

在使用编辑器之前,请尝试清除它。换句话说,执行以下操作:

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

我曾经遇到过和你一样的问题,以下方法对我有效。如果需要完整的代码示例,请查看我的博客文章

2
我遇到了类似的问题——我的字符串变量被保留了下来,但其他东西都没有。 作为一种简单粗暴的解决方法,我将所有内容都保存成字符串。

0

试试这个...

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

谢谢,但我也尝试了这种方法,但不知何故我的偏好设置被删除了。 - 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
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接