Android sqlite 存储来自列表的数据<LocalDate>

Android sqlite store data from List<LocalDate>

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

我得到的结果是:

|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

我追求的是

|tbl_id | days                                                          
-------------------
|1      | 2016-9-10
|2      | 2016-9-11
|3      | 2016-9-12

这是我目前所拥有的

方法天数

 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();
                }

方法插入

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();
            }

        }

改变

databaseHandler.insertDays(String.valueOf(days));

databaseHandler.insertDays(days);

第一个版本将您的列表转换为 String 表示并调用与您在此处显示的方法不同的 insertDays() 方法。