Android - Format Timestamp in ListView with Cursor Adapter
Asked Answered
Y

3

8

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?

Yevette answered 20/6, 2011 at 21:7 Comment(2)
What format are your timestamps in? Milliseconds?Forfar
@Glendon, Unix timestamps, made with PHP's time() / strtotime() functions.Yevette
F
16

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));
Forfar answered 21/6, 2011 at 1:15 Comment(4)
This worked perfectly, thank you so much! Would you happen to have a good resource on learning to work with Cursors / Adapters in Android? Thanks again!Yevette
No problem. I basically just copied this from my app and deleted some things. I haven't found a very comprehensive resource on adapters, but I learn most stuff here on Stack Overflow. newView() (inflates your layout) and bindView() (receives your layout and adds information) are the two main methods you need to override. getView() might be one you could research as well. Other than that, you can do whatever you want to your cursor data before displaying it in the ListView.Forfar
This may be a dumb question, but how do I tell this to populate a specific ListView in a specific file.xml? That is, I need to have some stuff in the top 33% of the screen, then have this fill up the ListView that takes up the bottom 2/3s. Am I missing something obvious? Thanks!Yevette
Nevermind, removing android:id="@android:id/empty" did the trick.Yevette
S
0

Store Unix epoch dates as INTEGER type in SQLite database. Then in Java, initialize them with new Date(value) (or value*1000, I'm not sure) and use SimpleDateFormat to format dates in list adapter.

I think that's the most convenient way for the limited information you've provided.

Slipover answered 21/6, 2011 at 0:0 Comment(2)
I know how to do that part, I meant more how do I change each one of the DATE_DATEs in the programDateCursor into the proper text before I pass them off to the new SimpleCursorAdapter? Should I just iterate over the cursor and format them each individually? If so, how? Or can I just send a filter function into SimpleCursorAdapter somehow? Or should I not be using a SimpleCursorAdapter at all?Yevette
I think you can implement your own cursor (or list) adapter. That will be more convenient.Slipover
H
0

I found it easiest to do the following:

SimpleDateFormat oldTime = new SimpleDateFormat("HH:mm:ss");
SimpleDateFormat newTime = new SimpleDateFormat("hh:mm:ss a");

String stringTime;

try {

        String reformattedStr = newTime.format(oldTime.parse(stringTime));
        } catch (ParseException e) {
        e.printStackTrace();
        }
Hermit answered 12/7, 2015 at 0:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.