0

I'm using SimpleCursorAdaptor and a ListView to display the values in my SQLite database rows. One of the values in my row is a date (column 'date'). Rather than display this date I need to run this through a method that will return another string based on what the date is. This is the value I want displayed in my list rather than the actual value taken straight from the Database.

In short I wish to display all values from my database table row except for one, where I need to change it before displaying it.

Here is my code:

public class BinCollectionDayListActivity extends ListActivity{
    //
    private static final String fields[] = { "name", "date", BaseColumns._ID };
    //
    public void onCreate(Bundle savedInstanceState) {
        //
        super.onCreate(savedInstanceState);
        //
        DatabaseHelper helper = new DatabaseHelper(this);
        SQLiteDatabase database = helper.getWritableDatabase();
        Cursor data = database.query("names", fields, null, null, null, null, null);
        CursorAdapter dataSource = new SimpleCursorAdapter(this, R.layout.binrow, data, fields, new int[] { R.id.name, R.id.date });
        dataSource.getCursor().requery();
        //
        ListView view = getListView();
        view.setHeaderDividersEnabled(true);
        setListAdapter(dataSource);
        //
        helper.close();
        database.close();
    }
}

As you can tell I am pretty new to Android development and would love to know what the best approach would be to achieving the desired result.

Thanks in advance,

Tony

TonyC
  • 1
  • Instead of directly using SimpleCursorAdapter. Construct separate array and populate you values to that array and pass that array as parameter to setListAdapter(...); – kosa Apr 24 '12 at 13:34

1 Answers1

0

Two options that I've used before:

Array Adapter (Preferred): Create an ArrayAdapter and populate the Cursor data into your ArrayAdapter. http://anujarosha.wordpress.com/2011/11/17/how-to-create-a-listview-using-arrayadapter-in-android/

ViewBinder: On your Cursor, you can setup/specify a ViewBinder where you can check the data that's about to be mapped, perform some logic on it, and spit out a different result if desired. This is probably exactly what you're looking for, but do consider the ArrayAdapter as it tends to give you better control and it's a pain to switch these things later on. Changing values from Cursor using SimpleCursorAdapter

Community
  • 1
  • 1
Gophermofur
  • 2,101
  • 1
  • 14
  • 14