存储/检索DateTime

时间:2012-09-17 11:04:29

标签: android sqlite datetime android-listview

我有一个应用程序正在检索一个日期,该日期以1970年的毫秒为单位存储为字符串,例如1324657734883

我有ListView显示此日期asis。我想在数据库中显示来自此字段的joda DateTime。我的视图显示了列表视图并使用startManagingCursor()对其进行了填充,因此我认为在将milisec格式填充到DateTime之前,无法将listview格式转换为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); } } }}。

有没有解决方法或者我必须存储DateTime,如果是这样,我需要声明存储此类数据的列类型是什么?

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

}

[UPDATE1]     公共类ViewTransactions扩展了NfcBaseActivity {

{{1}}

2 个答案:

答案 0 :(得分:2)

创建扩展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;
    }
}

然后用它代替默认值:

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

答案 1 :(得分:0)

使用android.Text.DateFormat

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

但您必须ArrayAdapter<String>使用ListView

如果您使用LoaderManager,而不是从您的活动中管理游标,这将很容易。