Skip to the bottom if you just want to see the question without context
The android app I'm building has a simple table with three columns:
_id INTEGER PRIMARY KEY..., name TEXT, color INT
This table is called categories
. I load my categories from the database and feed them into a SimpleCursorAdapter
for use with a Spinner
like so:
String[] from = new String[] {
ListDbAdapter.KEY_CATEGORY_NAME,
ListDbAdapter.KEY_CATEGORY_COLOR };
int[] to = new int[] { R.id.categorySpinnerItem };
mCategorySpinnerAdapter = new SimpleCursorAdapter(this,
R.layout.category_spinner_item, categoryCursor, from, to);
mCategorySpinnerAdapter
.setViewBinder(new CategorySpinnerViewBinder());
mCategorySpinner.setAdapter(mCategorySpinnerAdapter);
I set a custom ViewBinder
because I want the category name to be the text of the spinner item, and the color to be the background color. My ViewBinder
looks like this:
private static final int NAME_COLUMN = 1;
private static final int COLOR_COLUMN = 2;
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
TextView textView = (TextView) view;
String name = cursor.getString(NAME_COLUMN);
int color = cursor.getInt(COLOR_COLUMN);
textView.setText(name);
textView.setBackgroundColor(color);
return true;
}
Here is my question (finally)
In the setViewValue method, what is columnIndex supposed to be doing? The documentation says "the column at which the data can be found in the cursor" but when I debug through setViewValue I hit it three times and columnIndex is always 1
.
I was expecting the debugger to get into setViewValue once for each entry in the from array, with a columnIndex first of 1
and then 2
. Or maybe once for each column in the query results.
The above code works, I can get the desired functionality but only because of my NAME_COLUMN and COLOR_COLUMN constants. I'd be really interested to hear an explanation of setViewValue and its parameters from someone more experienced with custom ViewBinders.