android– 共享首选项未保存

android– 共享首选项未保存,第1张

概述在我的活动中,我根据存储的首选项更新用户界面.updateUI的代码如下:privatevoidupdateUI(){//preferences=getSharedPreferences(Select.PREF_FILE_NAME,MODE_PRIVATE);preferences=PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

在我的活动中,我根据存储的首选项更新用户界面. 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 – 共享首选项未保存全部内容,希望文章能够帮你解决android – 共享首选项未保存所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1115369.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存