简体   繁体   English

如何在Android的共享首选项中保存多个数据

[英]how to save multiple data in shared preferences in android

my app contains a list of dates in which there is toggle button 我的应用程序包含一个日期列表,其中有一个切换按钮

    1.>on check it sets alarm and on uncheck it deletes the alarm

these is working normally but when i close aplication then alarm will automaically gets deleted so i want to save data in shared preferences when toggle button is clicked and delete particular data when btn is unchek from shared preferneces for this i am doing 这些工作正常,但是当我关闭应用程序时,警报将自动删除,因此当我想从共享偏好中取消btn时,我想在单击共享按钮时将数据保存在共享首选项中

SharedPreferences sharedPref = context.getSharedPreferences("com.example.dd.mydata", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("alarm"+cday+"_"+month,cday);
editor.commit();

where cday and month is alarm date and month 其中cday和month是警报日期和月份

on recieve i have to fetch these data and match with date so that alarm can ring 收到后,我必须获取这些数据并与日期匹配,以便闹钟响起
so on recive i am doing this 所以在接受我的过程中

SharedPreferences sharedPref = arg0.getSharedPreferences("com.example.dd.mydata", Context.MODE_PRIVATE);
        String name = sharedPref.getString("alarm"+cday+"_"+cmonth, null);

in these way i am achiving the value of shared preferences in both class but it is working for single toggle button clicking more than 2 button **overrites the also tell me how can i check whether the current dates exists or not in shared prefernces 通过这种方式,我在两个类中都实现了共享首选项的值,但它适用于单击两个以上的单个切换按钮** overrites还告诉我如何检查当前日期是否存在于共享偏好中

You can call SharedPreferences.contains(String key) method. 您可以调用SharedPreferences.contains(String key)方法。

For example, contains("alarm"+cday+"_"+cmonth) 例如, contains("alarm"+cday+"_"+cmonth)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM