如何将日期发送到光标?

时间:2019-06-30 20:37:27

标签: java android date android-cursor

我无法用光标获取日期,我测试了所有可能性,但找不到解决方法。

我都尝试过。 问题是我想获得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

2 个答案:

答案 0 :(得分:2)

通过更改固定:-

c.getString(c.getString(c.getColumnIndex(KEY_DATE_TREATMENT)));

收件人:-

c.getString(c.getColumnIndex(KEY_DATE_TREATMENT));

当您将c.getString(c.getColumnIndex(KEY_DATE_TREATMENT))用作arg时,尝试使用带有 String arg而不是 int arg的c.getString()。 arg返回 String (要获取的 数据 )而不是 int

链接说:-

  

getString

public abstract String getString (int columnIndex)
     

参数

     

columnIndex int:目标列的从零开始的索引。

c.getColumnIndex(KEY_DATE_TREATMENT) 获得 int ,即名称为列的列偏移量,其值为 KEY_DATE_TREATMENT 并在 getString 方法(与使用 getInt 的其他行一样)。

答案 1 :(得分:0)

您对getString()有额外的不必要的错误呼叫:

c.getString(c.getString(c.getColumnIndex(KEY_DATE_TREATMENT)))

更改为此:

        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.getColumnIndex(KEY_DATE_TREATMENT)));