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 8037743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:00:48+00:00 2026-06-05T03:00:48+00:00

I can’t seem to figure out this SimpleCursorAdapter , every time I fix one

  • 0

I can’t seem to figure out this SimpleCursorAdapter, every time I fix one error, another one pops up and if I follow the steps to fix that one, the first one comes up again. I feel like I’m going in circles here, so here’s the chunk of code I’m trying to debug, note, the first part is just creating the DB, but I figured anything could be helpful in figuring it out.

        SQLiteDatabase rpgDB = null;
        String classFields = " (classID, className, classHP)";

        try {
            rpgDB = this.openOrCreateDatabase("RpgDB", MODE_PRIVATE, null);
            rpgDB.execSQL("DROP TABLE IF EXISTS " + classTable);

            rpgDB.execSQL("CREATE TABLE IF NOT EXISTS " + classTable + " (classID INT(3), className TEXT, classHP INT(4));");
            rpgDB.execSQL("INSERT INTO " + classTable + classFields + " VALUES (1, 'Warrior', 10);");
            rpgDB.execSQL("INSERT INTO " + classTable + classFields + " VALUES (2, 'Rogue', 7);");
            rpgDB.execSQL("INSERT INTO " + classTable + classFields + " VALUES (3, 'Mage', 5);");


            String query = "SELECT className AS _id FROM " + classTable;
            Cursor cursor = rpgDB.rawQuery(query, null);
            rpgDB.close();

            int[] to = new int[] { R.id.classDropDown };
            String[] spinnerFields = new String[] { "_id" };
            /**
             * Test code to check value of cursor
             */ 
            int count = cursor.getCount();
            cursor.moveToFirst();
            while (cursor.isAfterLast() == false) 
            {
                String test  = cursor.getString(0);
                cursor.moveToNext();
            }

            SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.main, cursor, spinnerFields, to);
            int cursorcount = cursorAdapter.getCount();
            // cursorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            Spinner classDropDown = (Spinner) this.findViewById(R.id.classDropDown);
            classDropDown.setAdapter(cursorAdapter);
        } catch (Exception ex) {
            System.out.println("Exception: " + ex);
        }

Populating a Spinner from a SimpleCursorAdapter seems to be ridiculously troublesome.

EDIT: The error with this code is

Invalid statement in fill window

If I comment out the rpgDB.close() line

Spinner is not a view that can be bounds by this SimpleCursorAdapter

If I change the select statement and spinnerfields to (with the DB commented out):

String query = "SELECT className FROM " + classTable;
String[] spinnerFields = new String[] { "className" };

I get:

column '_id' does not exist

EDIT XML: Here is the main.xml (also id main) XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@color/white" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textSize="11pt"
        android:text="@string/hello"
        android:textColor="@color/baseTextColor" />

    <Spinner
        android:id="@+id/classDropDown"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</LinearLayout>

EXPLANATION: So if anyone comes upon this in the future, thanks to Luksprog, I understand this a bit better (and he showed me the fixes):

The android.R.id and android.R.layout “ids/layouts” are default xml layouts for Android components such as a TextField and Spinner “list” layouts. The Spinner object is basically NOT complete on its own. You need another XML portion to define how the list itself looks and another one for the lines (the text1 is a default android TextField).

So in short, assign your “to” field to a TextField or use one of the default Android ones such as android.id.text1, but in order to use that I believe you also have to use the layout that CONTAINS the default android field, andoid.R.layout (in my case, simple_spinner_item). Lastly, set the dropdownresource to your own xml or an android default (mine was android.R.layout.simple_spinner_dropdown_item).

  • 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-06-05T03:00:50+00:00Added an answer on June 5, 2026 at 3:00 am

    Check this:

    String query = "SELECT _id, className FROM " + classTable;
    Cursor cursor = rpgDB.rawQuery(query, null);
    int[] to = new int[] { android.R.id.text1 };
    String[] spinnerFields = new String[] { "className"};
    SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner, cursor, spinnerFields, to);
    cursorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    Spinner classDropDown = (Spinner) this.findViewById(R.id.classDropDown);
    classDropDown.setAdapter(cursorAdapter);
    

    Invalid statement in fill window

    You can’t close the database connection if you want to pull data from that Cursor.

    Spinner is not a view that can be bounds by this SimpleCursorAdapter

    The to int array represents the ids of the Views from the row layout file that you pass to the SimpleCursorAdapter to bind data from the from array (R.layout.main, I hope that you do have a layout file and just pass the activity’s layout). The id R.id.classDropDown that you used there represents a Spinner view and a SimpleCursorAdapter doesn’t know how to bind data to that(it can bind data to a TextView or ImageView). If you want a Spinner on each row(do you really want this? or do you want the default spinner row layout?) then use a custom adapter or a SimpleCursorAdapter.ViewBinder.

    column ‘_id’ does not exist

    Because you don’t have a column with this name in the table classTable. This column should be declared like:

    INTEGER PRIMARY KEY AUTOINCREMENT
    

    Maybe you can check this simple tutorial for Spinners.

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

Sidebar

Related Questions

Can't figure out how to do this in a pretty way : I have
Can't seem to figure out what's wrong with the simple getJSON call below. It's
Can I figure out if a function has already been assigned to an event?
Can't work out a way to make an array of buttons in android. This
Can anyone help me trying to find out why this doesn't work. The brushes
can I make my own headers in HTTP request ? e.g. This is normal
Can this be done? It seems like this should be possible. In general, I
Can somebody help me with this. There is HTML code: <h3> <label> <input type=checkbox
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Can I run this in a Windows command prompt like I can run it

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.