【发布时间】:2011-08-12 17:52:41
【问题描述】:
我需要将数据库中的时间字段(毫秒)转换为“MMM dd,yyyy HH:mm”并按升序列出导致列表视图的结果,但我很难让 DateTime 显示在我的列表显示。 这是我正在使用的代码:
数据库:
public Cursor getUserDateTimeLocations(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE_LOCATION, new String[] {KEY_ROWID,
KEY_LATITUDE, KEY_LONGITUDE, KEY_DATETIME,KEY_USERID,KEY_OBS}, KEY_USERID + "=" + rowId, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
Listview 现在我可以显示 DateTime 字段,但我需要转换每一行的字段,然后显示它,我不知道该怎么做:
private void dateTime() {
cursor = db.getUserDateTimeLocations(mLocationRowId);
startManagingCursor(cursor);
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
mTime= cursor.getLong(3);
Date resultdate = new Date(mTime);
String mDateTime = sdf.format(resultdate);
//Toast.makeText(this, mDateTime, Toast.LENGTH_LONG).show();
String[] from = new String[] { DBAdapter.KEY_DATETIME};<--How to do it here?
int[] to = new int[] {R.id.label};
SimpleCursorAdapter locations = new SimpleCursorAdapter(this, R.layout.user_row, cursor, from, to);
setListAdapter(locations);
}
【问题讨论】:
-
谢谢。有效!现在对如何在我的查询中按升序获取日期时间有任何想法吗?谢谢。
标签: android sqlite datetime listview