Question

I have a SimpleCursorAdapter which populates data form the SQLite database. It's working fine, however the "duration" column is given in milliseconds and I would like to divide it (duration / 1000) before displaying. Is that possible without storing another duration (in minutes) column in DB?

adapter:

 adapter = new SimpleCursorAdapter(getActivity(),
 R.layout.rec_list_items,
 db.selectAllRecords(),
 new String[] { "day", "hour", "minute", "duration", "link" },
 new int[] { R.id.textView_day, R.id.textView_hour, R.id.textView_minute, R.id.textView_duration, R.id.textView_link },
 CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

selectAllRecords:

public Cursor selectAllRecords() {
    String[] cols = new String[] {ID, DAY, HOUR, MINUTE, DURATION, LINK};
    Cursor mCursor = database.query(true, TABLE,cols,null
            , null, null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }

    return mCursor;
}
Was it helpful?

Solution

you have couple of options here:

one is to create a custom SimpleCursorAdapter and override setViewText(). in your implementation check if the textview in question is passed and set the view value to what you desire. you have to convert from String to int (or float, what you need) to apply your precision.

another one is to implement SimpleCursorAdapter.ViewBinder, set it as a binder in your adapter's setViewBinder and do your thing in the binder's setViewValue

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top