android – 共享首选项未保存

在我的活动中,我根据存储的首选项更新用户界面. 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,但没有运气.

解决方法:

我遇到了与getStringSet类似的问题,该文档在那里提供了帮助

Note that you must not modify the set instance returned by this call.
The consistency of the stored data is not guaranteed if you do, nor is
your ability to modify the instance at all.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...