我有一個應用程序檢索存儲爲一個字符串的日期從1970年開始毫秒,例如1324657734883
。存儲/檢索日期時間
我有一個ListView
顯示此日期asis。我想在數據庫中顯示來自此字段的joda DateTime
。我的視圖顯示列表視圖,並使用startManagingCursor()
填充它,所以我不認爲有任何方法將milisec
格式轉換爲DateTime
,然後將其填充到listview
。
有沒有辦法解決這個問題,還是我必須存儲一個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從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);
}
延伸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;
}
}
}
有辦法解決它,但你需要用你的自己的適配器。 –
@aleks G嗨,好的是,使用類似自定義適配器的東西?你想回答,我會接受嗎? – turtleboy
試試這個 - http://stackoverflow.com/questions/8166497/custom-adaptor-for-list-view –