Jos*_*osh 8 java android timestamp date android-listview
我正在使用SimpleCursorAdapter来填充Android ListView,并且想知道我应该如何获取从数据库获得的所有时间戳,每个时间戳在"DATE_DATE"到人类可读日期,也许使用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);
Run Code Online (Sandbox Code Playgroud)
我没有做过很多Java工作,所以有更好的方法/任何方式来做到这一点吗?除了事先将预先格式化的日期存储在数据库中,这是什么?
Gle*_*ger 16
您将不得不创建自定义CursorAdapter以便能够设置时间戳的格式.
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);
}
}
Run Code Online (Sandbox Code Playgroud)
该列表来改变String format自己的喜好是在这里.
然后你用这个适配器
Cursor programDateCursor = mDbAdapter.loadProgramDates();
startManagingCursor(programDateCursor);
setListAdapter(new MyAdapter(this, programDateCursor));
Run Code Online (Sandbox Code Playgroud)