简体   繁体   English

在Android中存储每天数据的最佳方式?

[英]Best way to store data for each day in Android?

I need to display a calendar to the user of my app, there is a default rate (int) for each day and when he/she selects a date give an option to customize the rate (int) for that day.我需要向我的应用程序的用户显示一个日历,每天都有一个默认费率 (int),当他/她选择日期时,可以选择自定义当天的费率 (int)。

To display the calendar I am using https://github.com/prolificinteractive/material-calendarview要显示我使用的日历https://github.com/prolificinteractive/material-calendarview

To store the rate for each day :要存储每天的费率:

A different variable stores the general rate for each day until the user overrides it.一个不同的变量存储每天的一般费率,直到用户覆盖它。

Where I am confused is what's the best way to store this kind of data ?我感到困惑的是存储此类数据的最佳方法是什么? Can I use http://developer.android.com/intl/es/reference/java/util/Calendar.html ?我可以使用http://developer.android.com/intl/es/reference/java/util/Calendar.html吗? If so can you point me to a tutorial or something如果是这样,你能给我指一个教程或其他东西吗

I don't know Android, but in Java…我不知道 Android,但在 Java 中......

Map地图

My first thought is to create a Map with the date object as key and the override int as value.我的第一个想法是创建一个Map ,以日期对象为键,以覆盖int为值。 Probably a SortedMap .可能是SortedMap See the Tutorial .请参阅教程

Map: date → integer映射:日期 → 整数

You can loop or query the date keys to apply their values to your calendar.您可以循环或查询日期键以将它们的值应用于您的日历。

In a Collection, you will need to use anInteger object rather than an int primitive.在集合中,您将需要使用Integer对象而不是int原语。 Auto-boxing will do that conversion work for you. 自动装箱将为您完成转换工作。

Serialization序列化

Then serialize that Map to storage every time it is edited, that is, every time the user overrides the default rate.然后每次编辑时将该 Map序列化到存储中,即每次用户覆盖默认速率时。 Or do so when your app quits.或者在您的应用退出时执行此操作。

When launching your app, restore the Map from storage.启动应用程序时,从存储中恢复地图。

You could serialize by using Java Serialization if supported in Android.如果 Android 支持,您可以使用Java 序列化进行序列化 See this Question, Serializing and deserializing a map with key as string .请参阅此问题,序列化和反序列化具有键为 string 的映射

But I would just write a plain text file.但我只会写一个纯文本文件。 The Apache Commons CSV library handles the grunt work of reading/writing Tab-delimited files (TDF) or CSV files. Apache Commons CSV库处理读取/写入制表符分隔文件 (TDF)CSV文件的繁重工作。

I suggest writing your date values to strings using the sensible and standard format of ISO 8601 : YYYY-MM-DD .我建议使用ISO 8601的合理和标准格式将日期值写入字符串: YYYY-MM-DD

Tip: Consider using a LocalDate object from Joda-Time or the backport of java.time instead of java.util.Date/.Calendar as the key.提示:考虑使用来自 Joda-Time 的LocalDate对象或 java.time 的 backport 而不是 java.util.Date/.Calendar 作为键。

Time Zone时区

Be aware that the meaning of a date depends on time zone.请注意,日期的含义取决于时区。 For example, a new day dawns earlier in Paris than in Montréal where it is still 'yesterday'.例如,新的一天在巴黎比在仍然是“昨天”的蒙特利尔早。 If the context of your app goes beyond one default time zone, you may need to record the time zone applicable when your user was making her choices about overrides.如果您的应用程序的上下文超出了一个默认时区,您可能需要记录当您的用户就覆盖做出选择时适用的时区。

Database数据库

If you had a zillion values that do not need to all be in memory, then use a database such H2 or SQLite.如果您有无数个不需要都在内存中的值,那么请使用H2或 SQLite 等数据库。

For a small set of data, use the Map + serialization described above.对于一小组数据,使用上面描述的 Map + 序列化。

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

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