Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3400530
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:54:15+00:00 2026-05-18T04:54:15+00:00

Skip to the bottom if you just want to see the question without context

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T04:54:16+00:00Added an answer on May 18, 2026 at 4:54 am

    In the source of SimpleCursorAdapter, the setViewValue is called in bindView :

    bound = binder.setViewValue(v, cursor, from[i]);
    

    where the third param from[i], which is the interesting one, is an iteration over an int[], which represents the column indexes used. However the index for [i] for the iteration itself comes from the int[] to which is passed to the constructor of the adapter, and in your case, it has only 1 item – R.id.categorySpinnerItem

    EDIT: In two words, the String[] and the int[] should be equivalent, same size and in same order – for each column name you need an int R.id… first view id will be connected to the first column id with from[0], second with from[1] and so on, and if you pass 10 columns, but you have only 3 R.id-s, you will get only to from[2] 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Skip to bottom for question, but first, a little context. So I have been
Skip to the bottom for the question; this is just some extra info I
[Frontmatter] (skip this if you just want the question) : I'm currently looking at
The Background (skip to the bottom if you want the question) Recently I upgraded
TLDR; skip to the bottom paragraph for the main question. I'll try to keep
NOTE: skip to bottom for real question Hey guys, right now I am in
Skip to My Code at the bottom of the question to get straight to
Please Skip to Update #2 at the Bottom if you don't want to read
Edit : See my full answer at the bottom of this question. tl;dr answer
You can skip to the bottom line if you don't care about the background:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.