Storing/retrieving DateTime

China☆狼群 提交于 2019-12-13 02:56:15

问题


I've an app that is retrieving a date that is stored as a string as a millisecs from 1970, eg 1324657734883.

I have a ListView that displays this date asis. I'd like to display the joda DateTime from this field in the database. My view displays the listview and populates it by using startManagingCursor() so i don't think there is any way of converting the milisec format to a DateTime before it is populated to the listview.

Is there a way around this or do i have to store a DateTime and if so what is the column type i need to declare to store this type of data?

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    nfcscannerapplication = (NfcScannerApplication) getApplication();

    // setup UI
    setContentView(R.layout.viewtransactions);
    setTitle(getCarername() + " has completed " + nfcscannerapplication.loginValidate.getNumberOfTransactions() + " visits today");
    //transactionCount = (TextView)findViewById(R.id.textviewtransactionsfordaycount);
    viewTransactions = (ListView) findViewById(R.id.listviewtransactions);

    //transactionCount.setText("You have completed 6 transactions today");

    // get data
    cursor = nfcscannerapplication.loginValidate.queryAllFromTransactions();
    startManagingCursor(cursor);

    // setup adapter and show the data




    String[] from = { 
            LoginValidate.C_NAME, LoginValidate.C_TAG_SCAN_TIME,
            LoginValidate.C_TAG_SCAN_TIME};
    int[] to = { R.id.rowcarername, R.id.rowsignedinoutstatus, R.id.rowsenttoserverat };

    adapter = new SimpleCursorAdapter(nfcscannerapplication, R.layout.rowdataactual,
            cursor, from, to);
    viewTransactions.setAdapter(adapter);


}

}

.

[update1] public class ViewTransactions extends NfcBaseActivity{

private static final String TAG = ViewTransactions.class.getSimpleName();

NfcScannerApplication nfcscannerapplication;
Cursor cursor;
ListView viewTransactions;
SimpleCursorAdapter adapter;
MyAdapter myAdapter;
//TextView transactionCount; //now written to status bar

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    nfcscannerapplication = (NfcScannerApplication) getApplication();

    // setup UI
    setContentView(R.layout.viewtransactions);
    setTitle(getCarername() + " has completed " + nfcscannerapplication.loginValidate.getNumberOfTransactions() + " visits today");
    //transactionCount = (TextView)findViewById(R.id.textviewtransactionsfordaycount);
    viewTransactions = (ListView) findViewById(R.id.listviewtransactions);

    //transactionCount.setText("You have completed 6 transactions today");

    // get data
    cursor = nfcscannerapplication.loginValidate.queryAllFromTransactions();
    startManagingCursor(cursor);

    // setup adapter and show the data




    String[] from = { 
            LoginValidate.C_NAME, LoginValidate.C_TAG_SCAN_TIME,
            LoginValidate.C_TAG_SCAN_TIME};
    int[] to = { R.id.rowcarername, R.id.rowsignedinoutstatus, R.id.rowsenttoserverat };

    myAdapter = new MyAdapter(nfcscannerapplication, R.layout.rowdataactual,
            cursor, from, to);
    viewTransactions.setAdapter(adapter);


}


class MyAdapter extends SimpleCursorAdapter {

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

    @Override
    public
    View getView(int position, View convertView, ViewGroup parent) {
        View v = super.getView(position, convertView, parent);
        if(v == null)
            return null;

        Cursor c = (Cursor)getItem(position);
        String val = c.getString(c.getColumnIndex(LoginValidate.C_TAG_SCAN_TIME));
        Date dt = new Date(Long.parseLong(val));
        SimpleDateFormat df = new SimpleDateFormat("dd MMMM yyyy");
        String res = df.format(dt);

        ((TextView)v.findViewById(R.id.rowsignedinoutstatus)).setText(res);
        ((TextView)v.findViewById(R.id.rowsenttoserverat)).setText(res);

        return v;
    }
}

}

回答1:


Create a custom adapter that extends SimpleCursorAdapter:

class MyAdapter extends SimpleCursorAdapter {
    @Override
    View getView(int position, View convertView, ViewGroup parent) {
        View v = super.getView();
        if(v == null)
            return null;

        Cursor c = (Cursor)getItem(position);
        String val = c.getString(c.getColumnIndex(LoginValidate.C_TAG_SCAN_TIME));
        Date dt = new Date(Long.parseLong(val));
        SimpleDateFormat df = new SimpleDateFormat("dd MMMM yyyy");
        String res = df.format(val);

        ((TextView)v.findViewById(R.id.rowsignedinoutstatus)).setText(res);
        ((TextView)v.findViewById(R.id.rowsenttoserverat)).setText(res);

        return v;
    }
}

Then use it in place of the default one:

adapter = new MyAdapter(nfcscannerapplication, R.layout.rowdataactual, cursor, from, to);



回答2:


Use DateFormat from android.Text.DateFormat

public static CharSequence getTimeStamp(long milliseconds) {
    Date d = new Date(milliseconds);
    return DateFormat.format("EEEE, MMMM dd, yyyy h:mm:ss aa", d);
}

But you will have to Use ArrayAdapter<String> for your ListView.

This will be easy if you use LoaderManager, instead of managing cursors from your Activity.



来源:https://stackoverflow.com/questions/12457926/storing-retrieving-datetime

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