Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing values from Cursor using SimpleCursorAdapter

I have database table with the columns {Name, Time (UTC format) , Latitude, Longitude}

I display the table using a ListActivity with a SimpleCursorAdapter.

I would like that the column Time show the time in a human readable format (13-07-2010 10:40) rather than in UTC format (18190109089).

How can I specify that the values from column Time need some filtering/adaptation?

POSSIBLE SOLUTION (with a problem):

SimpleCursorAdapter offers the method:

setCursorToStringConverter(SimpleCursorAdapter.CursorToStringConverter cursorToStringConverter);

to specify how a class that is able to convert a Cursor to CharSequence (convertToString(Cursor cursor). Anyway I don't know in which format should be the return CharSequence paramater!

like image 393
Giorgio Avatar asked Aug 31 '10 12:08

Giorgio


4 Answers


The simplest way to format a cursor value is to use SimpleCursorAdapter.setViewBinder(..):

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list, cursor,
            new String[] { Definition.Item.TITLE, Definition.Item.CREATE_DATE }, new int[] { R.id.title, R.id.createDate});

adapter.setViewBinder(new ViewBinder() {

    public boolean setViewValue(View aView, Cursor aCursor, int aColumnIndex) {

        if (aColumnIndex == 2) {
                String createDate = aCursor.getString(aColumnIndex);
                TextView textView = (TextView) aView;
                textView.setText("Create date: " + MyFormatterHelper.formatDate(getApplicationContext(), createDate));
                return true;
         }

         return false;
    }
});
like image 76
Hendrik Avatar answered Oct 24 '22 05:10

Hendrik


i also had the same problem after long struggle finally i found answer :) ( see below )

use setViewText (TextView v, String text)

for example

SimpleCursorAdapter shows = new SimpleCursorAdapter(this, R.layout.somelayout, accountCursor, from, to)
{
 @Override
 public void setViewText(TextView v, String text) {
 super.setViewText(v, convText(v, text));
}    
};

private String convText(TextView v, String text)
{

 switch (v.getId())
 {
 case R.id.date:
             String formatedText = text;
             //do format
            return formatedText;
        }
return text;
}
like image 32
Manas Avatar answered Oct 24 '22 03:10

Manas


You can use setViewBinder(), or subclass SimpleCursorAdapter and override bindView().

like image 4
CommonsWare Avatar answered Oct 24 '22 04:10

CommonsWare


You can use SQLite syntax on that column to format the date.

Something like this will do it

SELECT strftime('%d-%m-%Y %H:%M',1092941466,'unixepoch');

SELECT strftime('%d-%m-%Y %H:%M',timecol,'unixepoch');
like image 4
Pentium10 Avatar answered Oct 24 '22 03:10

Pentium10