简体   繁体   中英

How can I do for send date to cursor?

I can't get my Date with my cursor, I test all possibilities but I can't find a solution.

I have all tried. The problem is the 4 line where I want to get the KEY_DATE_TREATMENT

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

    }

The error that I have currently is :

getString(int) in Cursor cannot be applied to Java.lang.String

Fix by change :-

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

To :-

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

You trying to use c.getString() , with a String arg, not an int arg as you putting c.getString(c.getColumnIndex(KEY_DATE_TREATMENT)) as the arg. The arg return String ( data you want getting ) not int .

Link say :-

getString

public abstract String getString (int columnIndex)

Parameters

columnIndex int: the zero-based index of the target column.

c.getColumnIndex(KEY_DATE_TREATMENT) gets an int , the column offset of the column that is name with the value of KEY_DATE_TREATMENT and work in getString method (like other lines that use getInt ).

You have an extra unnecessary and wrong call to getString() :

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

change to this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM