问题描述
我使用的是SimpleCursorAdapter来填充一个Android ListView控件,并想知道我应该如何去使用的SimpleDateFormat获得所有我从数据库中获取的时间戳,每一个在DATE_DATE为人类可读的日期,也许?
I am using a SimpleCursorAdapter to populate an Android ListView, and was wondering how I should go about getting all of the timestamps I get from a database, each in "DATE_DATE" into human readable dates, maybe using SimpleDateFormat?
Cursor programDateCursor = mDbAdapter.loadProgramDates();
startManagingCursor(programDateCursor);
String[] from = new String[]{ "DATE_DATE" };
int[] to = new int[]{ R.id.text1 };
SimpleCursorAdapter programDates =
new SimpleCursorAdapter(this, R.layout.program_date,
programDateCursor, from, to);
setListAdapter(programDates);
我没有做过与Java很多工作,所以有没有更好的方法/没有办法做到这一点?除了存储preformatted日期前手的数据库,那是什么?
I've not done much work with Java, so is there a better way / any way to do this? Other than storing the preformatted dates in the database before hand, that is?
推荐答案
你将不得不创建一个自定义的CursorAdapter才能够格式化您的时间戳。
You're going to have to create a custom CursorAdapter to be able to format your timestamps.
public class MyAdapter extends CursorAdapter {
private final LayoutInflater mInflater;
public MyAdapter(Context context, Cursor cursor) {
super(context, cursor, false);
mInflater = LayoutInflater.from(context);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return mInflater.inflate(R.layout.program_date, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
long time = cursor.getLong(cursor.getColumnIndex("DATE_DATE")) * 1000L;
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(time);
String format = "M/dd h:mm a";
SimpleDateFormat sdf = new SimpleDateFormat(format);
String dateString = sdf.format(cal.getTime());
((TextView) view.findViewById(R.id.text1)).setText(dateString);
}
}
清单到字符串格式
更改为自己喜欢的地方的。
The list to change the String format
to your liking is here.
然后你会使用这个适配器
You'd then use this adapter with
Cursor programDateCursor = mDbAdapter.loadProgramDates();
startManagingCursor(programDateCursor);
setListAdapter(new MyAdapter(this, programDateCursor));
这篇关于Android的 - 格式的时间戳的ListView中使用光标适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!