【发布时间】:2019-07-01 09:50:24
【问题描述】:
我无法用光标获取日期,我测试了所有可能性,但找不到解决方案。
我都试过了。
问题是我想得到KEY_DATE_TREATMENT的第4行
public List<Treatment> getTreatmentsList(){
List<Treatment> tList = new ArrayList<>();
Cursor c = getTreatments();
if (c.moveToFirst())
{
do {
SimpleDateFormat dateFormatprev = new SimpleDateFormat("yyyy-MM-dd");
Date d = new Date();
try {
d = dateFormatprev.parse(c.getString(c.getColumnIndex(KEY_DATE_TREATMENT)));
}
catch (ParseException e){
}
Treatment data = new Treatment(c.getInt(c.getColumnIndex(KEY_ID_TREATMENT)),
c.getInt(c.getColumnIndex(KEY_IDPARCEL_TREATMENT)),
c.getInt(c.getColumnIndex(KEY_IDTREATMENTPRODUCT_TREATMENT)),
c.getInt(c.getColumnIndex(KEY_IDWEATHER_TREATMENT)),
c.getString(c.getString(c.getColumnIndex(KEY_DATE_TREATMENT)));
tList.add(data);
}
while (c.moveToNext());
}
c.close();
}
我目前遇到的错误是:
Cursor 中的getString(int) 不能应用于Java.lang.String
【问题讨论】:
-
Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. 见:How to create a Minimal, Complete, and Verifiable example。
-
仅供参考,
java.util.Date、java.util.Calendar和java.text.SimpleDateFormat等麻烦的日期时间类现在已被 java.time 类所取代。大多数 java.time 功能在 ThreeTen-Backport 项目中被反向移植到 Java 6 和 Java 7。进一步适用于ThreeTenABP 中的早期 Android (How to use ThreeTenABP…。
标签: java android date android-cursor