Displaying a formatted date from DB

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Displaying a formatted date from DB

Postby emooney » Sun Jan 31, 2010 7:21 am

I have a Date (Sat Jan 30 12:13:01 EST 2010) that I inserted into a SQLite DB and I want to format it when it's displayed in a view. I'm new to the Android platform and am slightly overwhelmed at the moment and need help.

When I use a SimpleCursorAdaptor object to pull my data out and display it, I don't have the chance to edit the data before it's displayed. I've seen examples where people are extending SimpleCursorAdaptor and they're overRiding getView() and I've also seen other examples where they are implementing SimpleCursorAdaptor.ViewBinder.

I've been unsuccessful with both approaches and I'm not even sure these are the approaches I should take for a seemingly small task. Can someone point me to a small example that can help me over this hump?

Thanks in advance for your help,
Eric
emooney
Junior Developer
Junior Developer
 
Posts: 14
Joined: Tue Jan 26, 2010 11:03 pm

Top

Postby emooney » Mon Feb 01, 2010 5:37 am

I figured it out but I'll let someone chime in to tell me if it's the best way. I was able to extrapolate what I was looking for from this article.

You need to implement SimpleCursorAdapter.ViewBinder. I'm only displaying two fields in my list; a date field and another text field 'reps'.

So, in my ListActivity class, here's my onCreate() method:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     public void onCreate(Bundle savedInstanceState) {
  2.         super.onCreate(savedInstanceState);
  3.         setContentView(R.layout.ex_summary_list);        
  4.         mDbHelper = new WorkoutDbAdapter(this);
  5.         mDbHelper.open();
  6.         fillData();
  7.         registerForContextMenu(getListView());
  8.     }
  9.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Then, here's fillData():
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     private void fillData() {  
  2.        
  3.         Bundle extras = getIntent().getExtras();
  4.         mRowId = extras != null ? extras.getLong(WorkoutDbAdapter.KEY_EX_ROWID) : null;
  5.  
  6.         class ShowViewBinder implements SimpleCursorAdapter.ViewBinder {
  7.            
  8.             private boolean retVal = true;
  9.          
  10.             //@Override
  11.             public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
  12.                
  13.                 // If we are on the Date column,
  14.                 if ( columnIndex == cursor.getColumnIndex(WorkoutDbAdapter.KEY_SESS_DATE)) {
  15.                        
  16.                         // get the string that represents the date
  17.                         String theDate = cursor.getString(cursor.getColumnIndex(WorkoutDbAdapter.KEY_SESS_DATE));                    
  18.                    
  19.                         Date myDate = new Date(theDate);
  20.                         SimpleDateFormat newDateFormat = new SimpleDateFormat("MM-dd-yyyy");
  21.                         StringBuilder dateString = new StringBuilder(newDateFormat.format(myDate) );
  22.                        
  23.                         TextView tv = (TextView) view;                                                          
  24.                     tv.setText(dateString.toString());                    
  25.                 }
  26.                
  27.                 if ( columnIndex == cursor.getColumnIndex(WorkoutDbAdapter.KEY_SESS_REPS)) {
  28.                        
  29.                         // get the string that represents the reps
  30.                         String theReps = cursor.getString(cursor.getColumnIndex(WorkoutDbAdapter.KEY_SESS_REPS));                    
  31.                                        
  32.                         TextView tv = (TextView) view;                                                          
  33.                     tv.setText(theReps);                    
  34.                 }
  35.                
  36.                 return retVal;
  37.             }
  38.         }      
  39.        
  40.        
  41.        
  42.         c = mDbHelper.fetchAllSessionsByExercise("" + mRowId);  
  43.         startManagingCursor(c);
  44.                
  45.         // Create an array to specify the fields we want to display in the list (only TITLE)
  46.         String[] from = new String[]{WorkoutDbAdapter.KEY_SESS_DATE, WorkoutDbAdapter.KEY_SESS_REPS};
  47.  
  48.         // and an array of the fields we want to bind those fields to (in this case just text1)
  49.         int[] to = new int[]{R.id.text1, R.id.text2};
  50.        
  51.        
  52.         // Now create a simple cursor adapter and set it to display
  53.         SimpleCursorAdapter exercises = new SimpleCursorAdapter(this, R.layout.ex_summary_row, c, from, to);
  54.         <span style="font-weight: bold">exercises.setViewBinder(new ShowViewBinder());</span>
  55.         setListAdapter(exercises);
  56.  
  57.     }
  58.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


    * The 'retVal' value is important because this turns the whole function on if 'true' and off if 'false'
    * Also towards the end, you need to call setViewBinder(new ShowViewBinder()) on the cursor object.
    * It looks like if you need to edit content for one of your fields in your view, you need to run all of your other fields through this too. See what I'm doing for the 'reps' field.

Hope this helps,
Eric
emooney
Junior Developer
Junior Developer
 
Posts: 14
Joined: Tue Jan 26, 2010 11:03 pm

Postby SamB09 » Tue Feb 02, 2010 4:55 pm

Hi slighylu off topic but with that date format in your sql lite do you know how to set a reminder for each of the times in your db?
SamB09
Developer
Developer
 
Posts: 39
Joined: Tue Oct 27, 2009 9:22 pm
Location: England

Postby jwei512 » Wed Feb 03, 2010 11:00 am

You can also try implementing a custom cursor adapter (these will probably come in handy during your time as a developer...)

I wrote a little on how to implement these so you can take a look at them here:

http://thinkandroid.wordpress.com/categ ... tutorials/
- jwei

http://thinkandroid.wordpress.com
Examples, Code, and Tutorials
jwei512
Senior Developer
Senior Developer
 
Posts: 135
Joined: Mon Jul 27, 2009 9:35 pm
Location: Palo Alto, CA

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 5 guests