Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将时间从数据库转换到ListView时出现问题_Android_Sqlite_Datetime_Listview - Fatal编程技术网

Android 将时间从数据库转换到ListView时出现问题

Android 将时间从数据库转换到ListView时出现问题,android,sqlite,datetime,listview,Android,Sqlite,Datetime,Listview,我需要将数据库中的时间字段(毫秒)转换为“MMM dd,yyyy HH:mm”和列表,以升序显示listview,并且我在获取在listview上显示的日期时间方面遇到了问题。 以下是我使用的代码: DB: Listview现在我可以显示DateTime字段,但我需要转换每行的字段,然后显示它,我不知道如何操作: private void dateTime() { cursor = db.getUserDateTimeLocations(mLocationRowId); star

我需要将数据库中的时间字段(毫秒)转换为“MMM dd,yyyy HH:mm”和列表,以升序显示listview,并且我在获取在listview上显示的日期时间方面遇到了问题。 以下是我使用的代码:

DB:

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

}
private void dateTime(){
cursor=db.getUserDateTimeLocations(mlLocationRowid);
开始管理游标(游标);
SimpleDataFormat sdf=新的SimpleDataFormat(“mm-dd,yyyy-HH:mm”);
mTime=cursor.getLong(3);
日期结果日期=新日期(mTime);
字符串mDateTime=sdf.format(resultdate);
//Toast.makeText(this,mDateTime,Toast.LENGTH_LONG).show();

String[]from=new String[]{DBAdapter.KEY_DATETIME};您所要做的就是覆盖CursorAdapter中的setViewText。 我的几行代码解决了相同的问题:

private class MyCursorAdapter extends SimpleCursorAdapter {

    public MyCursorAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to) {
        super(context, layout, c, from, to);

    }

    private Date d = new Date();

    @Override
    public void setViewText(TextView v, String text) {
        if (v.getId() == R.id.TrackTime) {
            d.setTime(Long.parseLong(text));
            text = d.toLocaleString();
        }
        super.setViewText(v, text);
    }
}

Hrmm看起来您可能需要使用自己的适配器。SimpleCrsorAdapter就是这样-一种将字段从DB绑定到某种文本视图的简单方法。有关良好的演练,请参阅链接。

如果这是SimpleCrsorAdapter的唯一特殊部分,以下内容将有所帮助。在setListAdapter调用之前添加以下代码:

locations.setViewBinder(new SimpleCursorAdapter.ViewBinder() {

        @Override
        public boolean setViewValue(final View view, final Cursor cursor, final int columnIndex) {
            if (columnIndex == 1) {
                TextView textView = (TextView) view;

                long millis = cursor.getLong(columnIndex);
                textView.setText(yourFormattingOfMillisToDateHere);

                return true;
            }

            return false;
        }

    } );

谢谢。成功了!现在知道如何在我的查询中按升序获取日期时间了吗?谢谢。
locations.setViewBinder(new SimpleCursorAdapter.ViewBinder() {

        @Override
        public boolean setViewValue(final View view, final Cursor cursor, final int columnIndex) {
            if (columnIndex == 1) {
                TextView textView = (TextView) view;

                long millis = cursor.getLong(columnIndex);
                textView.setText(yourFormattingOfMillisToDateHere);

                return true;
            }

            return false;
        }

    } );