【问题标题】:Problem converting time from database to ListView将时间从数据库转换为 ListView 的问题
【发布时间】: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


【解决方案1】:

如果这是您的 SimpleCursorAdapter 的唯一特殊部分,则以下内容会有所帮助。在您的 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;
        }

    } );

【讨论】:

    【解决方案2】:

    您所要做的就是在 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);
        }
    }
    

    【讨论】:

      【解决方案3】:

      嗯,看来您可能需要推出自己的适配器。 SimpleCursorAdapter 就是这样 - 一种将字段从数据库绑定到某种文本视图的简单方法。请参阅this 链接以获得良好的演练。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-05-28
        • 1970-01-01
        • 2017-12-31
        • 1970-01-01
        • 1970-01-01
        • 2013-03-02
        • 2013-05-04
        相关资源
        最近更新 更多