试图使一个自定义的CursorAdapter可以转换UnixTime为可读投入前的ListView [英] Trying to make a Custom CursorAdapter that converts UnixTime into something readable before putting in a ListView

查看:117
本文介绍了试图使一个自定义的CursorAdapter可以转换UnixTime为可读投入前的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做的东西,一个自定义的CursorAdapter,我正在写。对于我在做什么该计划包括一个SQLite数据库充分的日期和时间为一个报警熄灭。 (由于SQLite不居然有一个DATETIME数据类型,他们都只是被存储为INT之前转换成UNIX时间)。我发现的这个例子制作自定义光标适配器,并试图去适应它,这样它可能需要在Unix时间条目,并将其转换弄成人类可读。我已经打了一个有点路障,虽然。我不能确定,如果我有什么是我需要的。下面是我到目前为止有:

I'm trying to make a custom CursorAdapter for something that I'm writing. The plan for what I'm doing involves an SQLite databases full of dates and times for an alarm to go off. (Since SQLite doesn't actually have a DATETIME datatype, they're all just converted to Unix Time before being stored as INT.) I found this example of making a custom cursor adapter and tried to adapt it so that it could take in the Unix Time entries and convert them into something human-readable. I've hit a bit of a road-block, though. I'm unsure if what I have is what I need. Here's what I've got so far:

import java.util.GregorianCalendar;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filterable;
import android.widget.TextView;

/**
 * @author Dave Smith
 *
 */
public class DateTimeCursorAdapter extends SimpleCursorAdapter implements Filterable {

    private Context context;
    private int layout;

    /**
     * @param context
     * @param layout
     * @param c
     * @param from
     * @param to
     * @param flags
     */
    public DateTimeCursorAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to, int flags) {
        super(context, layout, c, from, to, flags);
        this.context = context;
        this.layout = layout;
    }
    public View newView(Context context, Cursor cursor, ViewGroup parent){
        Cursor c = getCursor();
        final LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(layout, parent, false);
        /*int nameCol = c.getColumnIndexOrThrow("ALARMS.DATETIME");
        String dateTime = c.getString(nameCol);
        dateTime = convertFromUnixTime(Long.parseLong(dateTime)).toString();
        TextView dateTimeText = (TextView) v.findViewById(R.id.dateandtime);
        if (dateTimeText != null){
            dateTimeText.setText(dateTime);
        }*/
        return v;       
    }
    public void bindView(Context context, Cursor c, View v){
        int nameCol = c.getColumnIndexOrThrow("ALARMS.DATETIME");
        String dateTime = c.getString(nameCol);
        dateTime = convertFromUnixTime(Long.parseLong(dateTime)).toString();
        TextView dateTimeText = (TextView) v.findViewById(R.id.dateandtime);
        if (dateTimeText != null){
            dateTimeText.setText(dateTime);
        }
    }
    private GregorianCalendar convertFromUnixTime(long unixTime)
    {
        GregorianCalendar cal = new GregorianCalendar();
    cal.setTimeInMillis(unixTime);
    return cal;
    }

}

这会允许我在从我的数据库中的所有各种UnixTime条目阅读并把它们出MM / DD / YYYY HH:MM格式(或任何当前区域接受)

Would this allow me to read in all the various UnixTime entries from my database and put them out in MM/DD/YYYY HH:MM format (or whatever the current locale accept)?

推荐答案

无需自定义适配器。让你的数据库的工作。

No need for a custom adapter. Make the database work for you.

从文档:

1.2日期和时间数据类型

" 1.2 Date and Time Datatype

SQLite没有一个存储类存储日期和/或时间预留。相反,SQLite的的内置日期和时间函数能够存储日期和时间为TEXT,REAL或INTEGER值:

SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:

TEXT为ISO8601字符串(YYYY-MM-DD HH:MM:SS.SSS)。 REAL为儒略日的数字,因为中午在格林威治天11月24日的数字,公元前4714根据proleptic公历。整数作为Unix的时间,因为秒数1970-01-01 00:00:00 UTC。

TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS"). REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar. INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.

应用程序可以选择存储日期和时间的任何格式和使用格式之间随意转换内置的日期和时间的函数。

Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions."

使用适当的SQLite的功能,以您的日期/时间转换它进入光标之前。

Use the appropriate SQLite functions to convert your date/time before it goes into the cursor.

这篇关于试图使一个自定义的CursorAdapter可以转换UnixTime为可读投入前的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆