0

I've an app that is retrieving a date that is stored as a string as a millisecs from 1970, eg 1324657734883.

I have a ListView that displays this date asis. I'd like to display the joda DateTime from this field in the database. My view displays the listview and populates it by using startManagingCursor() so i don't think there is any way of converting the milisec format to a DateTime before it is populated to the listview.

Is there a way around this or do i have to store a DateTime and if so what is the column type i need to declare to store this type of data?

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    nfcscannerapplication = (NfcScannerApplication) getApplication();

    // setup UI
    setContentView(R.layout.viewtransactions);
    setTitle(getCarername() + " has completed " + nfcscannerapplication.loginValidate.getNumberOfTransactions() + " visits today");
    //transactionCount = (TextView)findViewById(R.id.textviewtransactionsfordaycount);
    viewTransactions = (ListView) findViewById(R.id.listviewtransactions);

    //transactionCount.setText("You have completed 6 transactions today");

    // get data
    cursor = nfcscannerapplication.loginValidate.queryAllFromTransactions();
    startManagingCursor(cursor);

    // setup adapter and show the data




    String[] from = { 
            LoginValidate.C_NAME, LoginValidate.C_TAG_SCAN_TIME,
            LoginValidate.C_TAG_SCAN_TIME};
    int[] to = { R.id.rowcarername, R.id.rowsignedinoutstatus, R.id.rowsenttoserverat };

    adapter = new SimpleCursorAdapter(nfcscannerapplication, R.layout.rowdataactual,
            cursor, from, to);
    viewTransactions.setAdapter(adapter);


}

}

.

[update1] public class ViewTransactions extends NfcBaseActivity{

private static final String TAG = ViewTransactions.class.getSimpleName();

NfcScannerApplication nfcscannerapplication;
Cursor cursor;
ListView viewTransactions;
SimpleCursorAdapter adapter;
MyAdapter myAdapter;
//TextView transactionCount; //now written to status bar

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    nfcscannerapplication = (NfcScannerApplication) getApplication();

    // setup UI
    setContentView(R.layout.viewtransactions);
    setTitle(getCarername() + " has completed " + nfcscannerapplication.loginValidate.getNumberOfTransactions() + " visits today");
    //transactionCount = (TextView)findViewById(R.id.textviewtransactionsfordaycount);
    viewTransactions = (ListView) findViewById(R.id.listviewtransactions);

    //transactionCount.setText("You have completed 6 transactions today");

    // get data
    cursor = nfcscannerapplication.loginValidate.queryAllFromTransactions();
    startManagingCursor(cursor);

    // setup adapter and show the data




    String[] from = { 
            LoginValidate.C_NAME, LoginValidate.C_TAG_SCAN_TIME,
            LoginValidate.C_TAG_SCAN_TIME};
    int[] to = { R.id.rowcarername, R.id.rowsignedinoutstatus, R.id.rowsenttoserverat };

    myAdapter = new MyAdapter(nfcscannerapplication, R.layout.rowdataactual,
            cursor, from, to);
    viewTransactions.setAdapter(adapter);


}


class MyAdapter extends SimpleCursorAdapter {

    public MyAdapter(Context context, int layout, Cursor c, String[] from,
            int[] to) {
        super(context, layout, c, from, to);
    }

    @Override
    public
    View getView(int position, View convertView, ViewGroup parent) {
        View v = super.getView(position, convertView, parent);
        if(v == null)
            return null;

        Cursor c = (Cursor)getItem(position);
        String val = c.getString(c.getColumnIndex(LoginValidate.C_TAG_SCAN_TIME));
        Date dt = new Date(Long.parseLong(val));
        SimpleDateFormat df = new SimpleDateFormat("dd MMMM yyyy");
        String res = df.format(dt);

        ((TextView)v.findViewById(R.id.rowsignedinoutstatus)).setText(res);
        ((TextView)v.findViewById(R.id.rowsenttoserverat)).setText(res);

        return v;
    }
}

}
turtleboy
  • 8,210
  • 27
  • 100
  • 199
  • There is a way around it, but you'll need to use your own adapter for this. – Aleks G Sep 17 '12 at 11:07
  • @aleks G hi, ok is that using something like a custom adapter? Would you like to give an answer and i'll accept it? – turtleboy Sep 17 '12 at 11:10
  • try this - http://stackoverflow.com/questions/8166497/custom-adaptor-for-list-view – Mukesh Soni Sep 17 '12 at 11:11
  • @turtleboy Have a look at the answer. I haven't tested it, so you may need to tweak it a bit, but you should get the idea. – Aleks G Sep 17 '12 at 11:35
  • @AleksG hi, yep i kinda worked it out, i've updated post with ammended code but my listview is now empty. Have you any ideas why. have i implemented how you suggested? – turtleboy Sep 17 '12 at 12:19
  • @turtleboy Have you tried stepping with the debugger through the `getView` method? To make sure that it's called in the right place and processes the right values? – Aleks G Sep 17 '12 at 12:21
  • @AleksG erm no, i haven't used debugger before, i usually just use logging statments to logcat. I'll give it a try. – turtleboy Sep 17 '12 at 12:24

2 Answers2

2

Create a custom adapter that extends SimpleCursorAdapter:

class MyAdapter extends SimpleCursorAdapter {
    @Override
    View getView(int position, View convertView, ViewGroup parent) {
        View v = super.getView();
        if(v == null)
            return null;

        Cursor c = (Cursor)getItem(position);
        String val = c.getString(c.getColumnIndex(LoginValidate.C_TAG_SCAN_TIME));
        Date dt = new Date(Long.parseLong(val));
        SimpleDateFormat df = new SimpleDateFormat("dd MMMM yyyy");
        String res = df.format(val);

        ((TextView)v.findViewById(R.id.rowsignedinoutstatus)).setText(res);
        ((TextView)v.findViewById(R.id.rowsenttoserverat)).setText(res);

        return v;
    }
}

Then use it in place of the default one:

adapter = new MyAdapter(nfcscannerapplication, R.layout.rowdataactual, cursor, from, to);
Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • @turtleboy Oh, sorry, of course not; it should be returning the view. I've updated the answer. – Aleks G Sep 17 '12 at 12:14
  • i forgot to set the new custom adapter to the listview. the listview was still been passed the old adapter. it work fine thanks alot. – turtleboy Sep 17 '12 at 12:46
0

Use DateFormat from android.Text.DateFormat

public static CharSequence getTimeStamp(long milliseconds) {
    Date d = new Date(milliseconds);
    return DateFormat.format("EEEE, MMMM dd, yyyy h:mm:ss aa", d);
}

But you will have to Use ArrayAdapter<String> for your ListView.

This will be easy if you use LoaderManager, instead of managing cursors from your Activity.

S.D.
  • 29,290
  • 3
  • 79
  • 130