Trying to make a Custom CursorAdapter that converts UnixTime into something readable before putting in a ListView

拥有回忆 提交于 2019-12-11 17:59:31

问题


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

}

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


回答1:


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

From the docs:

" 1.2 Date and Time Datatype

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 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."


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



来源:https://stackoverflow.com/questions/10769722/trying-to-make-a-custom-cursoradapter-that-converts-unixtime-into-something-read

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!