Skip to content Skip to sidebar Skip to footer

Sort Db Data And Display In List View

I get data from my DB using cursor = db.query('WebLeadMaster', select, 'condition1='+conditionVal1+ ' and condition2='+ConditionVal2,null, null, null, 'RegistrationTime DESC'); I

Solution 1:

The easiest suggestion would be to save the date in a different format (but still saved as string) into the database. If you would save the data into SQLite’s default date format (YYYY-MM-DD HH:NN:SS), you can easily sort the dates.

To display the date in your format, you would only just need to reformat the date into the correct format.

Solution 2:

Well, i changed my table structure. I added another field "_id" to it. Set the property as AUTO INCREMENT to it, and sorted the list with respect to _id field.

Solution 3:

If you r using ORM you can sort the data by timestamp. ORM makes data insertion and data retrieval easier from database. You have to include jar files to your project to use ORM...

Solution 4:

since SQLite has no datetime type, store date type as LONG/INT/NUMERIC in SQLite (EPOCH time) it will be easier to sort

then add ViewBinder to Adapter

/*field in activity/fragment*/finalstaticSimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //or other format that you wana to show in ListView
        ...
        mAdapter.setViewBinder(newSimpleCursorAdapter.ViewBinder() {

            @OverridepublicbooleansetViewValue(View view, Cursor cursor,
                    int columnIndex) {
                finalintid= view.getId();
                switch (id) {
                    case R.id.id_of_textview_with_date_data:
                        finalCalendarcal= Calendar.getInstance();
                        cal.setTimeInMillis(cursor.getLong(columnIndex));
                        ((TextView) view).setText(sdf.format(cal.getTime()));
                        returntrue;
                }
                returnfalse;
            }
        });

... or as dennisg pointed store it as STRING/VARCHAR edit: in "yyyy-MM-dd HH:mm:ss" format

Post a Comment for "Sort Db Data And Display In List View"