Note that there are some explanatory texts on larger screens.

plurals
  1. POSome help understanding columnIndex in ViewBInder
    primarykey
    data
    text
    <p><strong>Skip to the bottom if you just want to see the question without context</strong></p> <p>The android app I'm building has a simple table with three columns:</p> <blockquote> <p><strong>_id</strong> INTEGER PRIMARY KEY..., <strong>name</strong> TEXT, <strong>color</strong> INT</p> </blockquote> <p>This table is called <code>categories</code>. I load my categories from the database and feed them into a <code>SimpleCursorAdapter</code> for use with a <code>Spinner</code> like so:</p> <pre><code>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); </code></pre> <p>I set a custom <code>ViewBinder</code> because I want the category name to be the text of the spinner item, and the color to be the background color. My <code>ViewBinder</code> looks like this:</p> <pre><code>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; } </code></pre> <p><strong>Here is my question (finally)</strong></p> <p>In the <em>setViewValue</em> method, what is <em>columnIndex</em> supposed to be doing? The documentation says <em>"the column at which the data can be found in the cursor"</em> but when I debug through <em>setViewValue</em> I hit it three times and <em>columnIndex</em> is always <code>1</code>.</p> <p>I was expecting the debugger to get into <em>setViewValue</em> once for each entry in the from array, with a columnIndex first of <code>1</code> and then <code>2</code>. Or maybe once for each column in the query results.</p> <p>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 <em>setViewValue</em> and its parameters from someone more experienced with custom ViewBinders.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload