0

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

我有一个ListView显示这个日期的。我想在数据库中显示DateTime此字段中的 joda。我的视图显示列表视图并通过使用填充它,startManagingCursor()所以我认为在填充milisec到.DateTimelistview

有没有办法解决这个问题,或者我是否必须存储一个 DateTime,如果是这样,我需要声明什么列类型来存储这种类型的数据?

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] 公共类 ViewTransactions 扩展 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;
    }
}

}
4

2 回答 2

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);
于 2012-09-17T11:27:06.200 回答
0

DateFormatandroid.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);
}

但是你必须使用ArrayAdapter<String>你的ListView.

LoaderManager如果您使用,而不是从 Activity 管理光标,这将很容易。

于 2012-09-17T11:17:08.600 回答