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);
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?
Once you have a database and tables defined, then we can get access to a Cursor by querying the database with rawQuery: Now, we can use the CursorAdapter in the Activity to display an array of items into the ListView: This will then trigger the CursorAdapter iterating through the result set and populating the list.
SimpleCursorAdapter are particularly important for showing data from Android cursors. Cursors can fetch values from SQLite database. So in turn, SimpleCursorAdapter are used when you want to display data from SQLite Database. To use SimpleCursorAdapter as a datasource to our Listview control follow this steps :
ListView is view group that displays a list of scrollable items which are insterted in list using Adapter that pulls data from array or database. Adapter works as an intermediate between the datasource and the AdapterView layout where Adapter retrieves the data. SimpleCursorAdapter are particularly important for showing data from Android cursors.
Android Formatting Strings Format a timestamp to string Example For full description of patterns, see SimpleDateFormat reference Date now = new Date(); long timestamp = now.getTime(); SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.US); String dateStr = sdf.format(timestamp);
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With