[英]How can i get SQLite to sort by date properly using YYYY-MM-DD format?
I'm making an app that needs to sort a Sqlite table of weeks by date and display just the date and the total hours worked that week in a listview. 我正在制作一个应用程序,需要按日期对Sqlite表进行排序,并在列表视图中显示该周的日期和总工时。
My activity extends listview and uses SimpleCursorAdapter to populate the listview. 我的活动扩展了listview,并使用SimpleCursorAdapter填充列表视图。 My date column is a TEXT field formatted like this "YYYY-MM-DD"
我的日期列是一个TEXT字段,格式为“YYYY-MM-DD”
Here is my database query function, I base my database helper class off of the google notepad example. 这是我的数据库查询功能,我的数据库助手类基于google记事本示例。
public Cursor getAllWeeks()
{
Cursor mCursor = mDb.query(true, DATABASE_TABLE, null, null, null, null, null, "weekdate ASC", null);
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}
And I hook it to the ListView like this: 我把它挂钩到ListView,如下所示:
public class WeekList extends ListActivity implements OnClickListener{
final static int DELETE_ID = Menu.FIRST + 1;
public static NotesDbAdapter DBadapter;
public static Cursor ListCursor;
static final String[] displaycolumns = {"weekdate", "weektotalhours"};
static final int[] listitemviews = {R.id.TextViewListItem1, R.id.TextViewListItem2};
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.weeklist);
Log.i("bendebug", "logtest");
DBadapter = new NotesDbAdapter(this);
DBadapter.open();
ListCursor = DBadapter.getAllWeeks();
this.setListAdapter(new SimpleCursorAdapter(this, R.layout.listitemlayouts, ListCursor,
displaycolumns, listitemviews));
findViewById(R.id.ButtonAddWeek).setOnClickListener(this);
//Activate the context menu
registerForContextMenu(getListView());
}
The problem I am having is that my cursor doesn't seem sorted properly, my weeklist looks like this: 我遇到的问题是我的光标似乎没有正确排序,我的周列表如下所示:
2010-7-19
2010-7-23
2010-7-24
2010-7-6
Instead of being properly sorted. 而不是正确排序。 I'm probably missing something really obvious, since I'm a total noob trying to teach myself.
我可能错过了一些非常明显的东西,因为我是一个试图自学的总菜鸟。
Obviously i shortened the code to make it more readable, since the rest of the activity is irrelevant AFAIK but I would be happy to post the complete code for the activity on request, it's really basic anyway. 显然我缩短了代码以使其更具可读性,因为活动的其余部分与AFAIK无关,但我很乐意根据请求发布活动的完整代码,无论如何它都是基本的。
使用前导零来表示月份和日期。
SQLite does not have built-in date fields; SQLite没有内置日期字段; you have to use
TEXT
(textual representation), INTEGER
(unix time) or REAL. 你必须使用
TEXT
(文本表示), INTEGER
(unix time)或REAL。 See: Datatypes in SQLite . 请参阅: SQLite中的数据类型 。
You can use leading zeroes (as dan suggested) or better yet, save them as UNIX timestamps in INTEGER
fields. 您可以使用前导零(如建议的那样)或更好,将它们保存为
INTEGER
字段中的UNIX时间戳 。 You can convert these back to human-readable dates using SQLite's date and time functions or Java's (or whatever you are using). 您可以使用SQLite的日期和时间函数或Java(或您正在使用的任何内容)将这些日期转换回人类可读日期。
I have had to do similar sorting. 我不得不做类似的排序。 I ended up storing dates as REAL values in SQLite as the number of milliseconds since Epoch Jan 1 1970 00:00 GMT.
我最终在SQLite中将日期作为REAL值存储为自1970年1月1日00:00 GMT以来的毫秒数。 You can then simply sort these numerically (larger number occurred later chronologically).
然后,您可以简单地对这些数字进行排序(较大的数字后来按时间顺序排列)。 You can then convert to strings using the SimpleDateFormat.
然后,您可以使用SimpleDateFormat转换为字符串。 Also allows you to do great things with time elements which helps-- http://developer.android.com/reference/java/text/SimpleDateFormat.html
还可以让你用时间元素来做很棒的事情 - http://developer.android.com/reference/java/text/SimpleDateFormat.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.