首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android存储来自List<LocalDate>的数据

Android存储来自List<LocalDate>的数据
EN

Stack Overflow用户
提问于 2016-09-30 20:22:48
回答 1查看 277关注 0票数 0

我试图将每个数据存储为数据库中的每一行。我设法存储了数据,但是它被存储为所有数据的一行。顺便说一下,我用的是import org.joda.time.LocalDate

我得到的结果是:

代码语言:javascript
复制
|tbl_id | days                                                          
--------------------------------------------------------------------------------------------------------------------------------
|1      | 2016-9-10, 2016-9-11, 2016-9-12 2016-9-13, 2016-9-14, 2016-9-15, 2016-9-16, 2016-9-17, 2016-9-18, 2016-9-19, 2016-9-20

我想要的是

代码语言:javascript
复制
|tbl_id | days                                                          
-------------------
|1      | 2016-9-10
|2      | 2016-9-11
|3      | 2016-9-12

到目前为止,我的情况如下

方法日

代码语言:javascript
复制
 String from = "2016-9-10";
    String to = "2016-9-20";

    SimpleDateFormat df = new SimpleDateFormat("MMMM dd, yyyy", Locale.US);
    SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);

                try {

                    String reformatDateFrom = myFormat.format(df.parse(from));
                    String reformatDateTo = myFormat.format(df.parse(to));
                    LocalDate start = LocalDate.parse(reformatDateFrom);
                    LocalDate end = LocalDate.parse(reformatDateTo);
                    List<LocalDate> days= new ArrayList<>();

                    while (!start.isAfter(end)) {
                        days.add(start);
                        start = start.plusDays(1);

                    }

                    Toast.makeText(this, "This" + days,
                            Toast.LENGTH_LONG).show();
                    databaseHandler.insertDays(days);
                } catch (ParseException pe) {
                    pe.printStackTrace();
                }

方法插入

代码语言:javascript
复制
public void insertDays(List<LocalDate> days) {
            int size = days.size();

            sqLiteDatabase = this.getWritableDatabase();
            try {
                for(int i = 0; i < size; i++) {
                    ContentValues contentValues = new ContentValues();
                    contentValues.put(KEY_DAYS, String.valueOf(days.get(i)));
                    sqLiteDatabase.insert(TABLE_DAYS, null, contentValues);
                }
                sqLiteDatabase.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
               sqLiteDatabase.close();
            }

        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-30 20:36:52

变化

代码语言:javascript
复制
databaseHandler.insertDays(String.valueOf(days));

代码语言:javascript
复制
databaseHandler.insertDays(days);

第一个版本将列表转换为String表示,并调用与这里显示的方法不同的insertDays()方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39799532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档