私は各データをデータベースに格納しようとしています。私はデータを保存することができましたが、すべてのデータに対して1つの行として保存されています。リストからAndroidのsqliteストアデータ<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
は、ここで私はこれまで
方法日
が持っているものですされて何イム:ところで、私は取得していますimport org.joda.time.LocalDate
結果は使用しています
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();
}
}
最初のコードスニペットはどのメソッドに含まれていますか?これを表示するには質問を編集してください。 –
'insertDays()'メソッドは(ほとんど)正しく見えます。 (あなたの質問に関係のない1つの問題は、データベースを 'finally'節で閉じなければならないことです。)この' insertDays() 'メソッドはいつ呼びますか? –
@ Code-Apprentice私はwhileメソッドのwhileループの後に呼び出します。 'databaseHandler.insertDays(String.valueOf(days)); ' –