Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Format Timestamp in ListView with Cursor Adapter

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?

like image 568
Josh Avatar asked Jun 20 '11 21:06

Josh


People also ask

How do I get the cursor of a listview in Android?

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.

What is simplecursoradapter in Android listview?

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 :

What is listview in Android with adapter?

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.

How do I format a timestamp in Android format?

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);


1 Answers

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));
like image 118
Glendon Trullinger Avatar answered Nov 15 '22 22:11

Glendon Trullinger