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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:13:01+00:00 2026-05-27T23:13:01+00:00

I have a problem on Android Spinner.In my application I created two Spinner on

  • 0

I have a problem on Android Spinner.In my application I created two Spinner on main layout. ‘State’ Spinner and ‘District’ Spinner. All the data on Spinner are stored in SQLite database. I want do display list of ‘District’ on second spinner depending on selection of particular ‘State’ in the first spinner.
Example: Suppose when I select Karnataka in the first spinner then application first retrieve all the district from SQLite database related to karnataka state and then it display on second Spinner.

For this I do all the database activity correctly means creating two table ‘state’ and ‘district’ in which one column in district table is foreign key which is refereed to one of the primary key of ‘state table’

db.execSQL("create table "+STATE_TABLE+" ("+
            STATE_ID+" integer primary key autoincrement not null, "+
                STATE_NAME+" text"+")");

    db.execSQL("create table "+DISTRICT_TABLE+" ("+DISTRICT_ID+
            " integer primary key autoincrement not null,"+DISTRICT_NAME
                +" text,"+STATE_ID+" integer, FOREIGN KEY("
                    +STATE_ID+") REFERENCES "+STATE_TABLE
                        +"("+STATE_ID+")"+")");

Now in the Activity Class:

spinnerState = (Spinner)findViewById(R.id.spinner1);
spinnerDistrict = (Spinner)findViewById(R.id.spinner2);
stateList = new ArrayList<String>();
districtList = new ArrayList<String>();

Suppose all the data are all ready stored in database.

Now I need to retrieve all the ‘State’ data from database and add it in the statelist which is ArrayList.

Cursor stateCursor = database.query(STATE_TABLE, new String[]{STATE_ID, STATE_NAME},
            null, null, null, null, STATE_NAME);
    stateCursor.moveToFirst();

    if(! stateCursor.isAfterLast()){
        do{
            int id = stateCursor.getInt(0);
            String stateName = stateCursor.getString(1);
            stateList.add(stateName);
        }while(stateCursor.moveToNext());
    }
    stateCursor.close();

after this I create one ArrayAdapter and put this state list into this.

spinnerState.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, stateList));

Next i put the following code in the activity class:

spinnerState.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View v,
                int pos, long id) {

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }
    });

Now My problem is here:

  1. How I get the StateId for executing the select query for taking all the district related to particular state in database.
  2. How the adapter will generate for District.
  3. where I put all these code.

Here what I need creating the districtList after getting the value from state Spinner.

Similar Question which asked earlier in this website, what they do:
they already create two adapter for two spinner and then apply setOnItemSelectedListener.
Please Help me because here my mind totally stop working.
I refer lot of book and website but not they even mention these type of problem.

  • 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-27T23:13:01+00:00Added an answer on May 27, 2026 at 11:13 pm

    I see a couple of solutions here:

    Option 1:

    Instead of declaring stateList as an ArrayList<String>, create a custom POJO StateInfo and designate stateList as ArrayList<StateInfo>

    public class StateList {
        private int id;
        private String stateName;
    
        //Constructors, getters and setters
    }
    

    Then,

    if(! stateCursor.isAfterLast()){
        do{
            int id = stateCursor.getInt(0);
            String stateName = stateCursor.getString(1);
            stateList.add(new StateInfo(id, stateName));
        }while(stateCursor.moveToNext());
    }
    

    Now, you can do this:

    public void onItemSelected(AdapterView<?> parent, View v,
            int pos, long id) {
        int id = stateList.get(pos).getId();
        //Use This ID to construct your next SQLite query; and populate the district spinner.
    }
    

    If you do this, you will have to create a custom ArrayAdapter and implement the getView() method. Look at the Android docs for how to do this.

    Option 2:

    Why do you even need the stateID? I suppose you can write a Query to get the districts list given a state Name only. In that case, you can retain the stateList as an ArrayList<String> and you don’t even need the StateInfo class at all. Just do this:

    public void onItemSelected(AdapterView<?> parent, View v,
            int pos, long id) {
        String stateName = stateList.get(pos);
        //Use this name to construct your next SQLite query; and populate the district spinner.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to make a simple layout in android but have problem with the
I have an interesting problem being reported to me from an android application I
I have this problem in android. i have a main activity who calls a
I am developing in android application in which i have to make a layout
i have a spinner an two buttons on my Android app, the spinner is
I have Created a database in Sqllite Android Application and I tried to add
I have a strange problem in my android application. I am starting one activity
I have a problem with my android-app. I want to save a Data-list in
I have problem with return statment >.< I want to store all magazine names
I have problem when I try insert some data to Informix TEXT column via

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.