Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Option 1: Like @nguyendat said, for performance, you could store the formatted date in the database, as well as the un-formatted version, to give you the most flexibility. If in the same table though, this would violate 2nd normal form because of the redundancy, and you would have to be careful in your code to update all of the data in the row. To implement this, put your conversion code in your DBAdapter, in the insert command.</p> <p>Option 2: Create a class for your date</p> <pre><code>public class FormattedDate { private int oneDate; public Punch (int unformattedDate) { oneDate = unformattedDate; } // ends constructor @Override public String toString() { //put your conversion code here return myFormattedDate; }} </code></pre> <p>This has the added benefit of a proper place to put any other code for comparisons or conversions.</p> <p>Inside your DBAdapter, change your query to this</p> <pre><code>public ArrayList&lt;FormattedDate&gt; fetchAllItems() { ArrayList&lt;FormattedDate&gt; results = new ArrayList&lt;FormattedDate&gt;(); Cursor c = db.rawQuery("SELECT MY_UNFORMATTED_DATE FROM yourTable", null); if (c.getCount() &gt; 0) { c.moveToFirst(); do { results.add( new FormattedDate(c.getInt(c.getColumnIndex(MY_UNFORMATTED_DATE)))); } while (c.moveToNext()); } c.close(); return results; } </code></pre> <p>This returns an ArrayList of FormattedDate objects</p> <p>Finally, this would populate a listview</p> <pre><code>setContentView(R.layout.my_list_view); ArrayList&lt;FormattedDate&gt; dateArray = mDBHelper.fetchAllItens(); ArrayAdapter&lt;FormattedDate&gt; dateAdapter = new ArrayAdapter&lt;FormattedDate&gt; (getApplicationContext(), R.layout.list_item, dateArray); setListAdapter(dateAdapter); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload