Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: value of one spinner depends on value of other spinner
    primarykey
    data
    text
    <p>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.</p> <p>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'</p> <pre><code>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+")"+")"); </code></pre> <p>Now in the Activity Class:</p> <pre><code>spinnerState = (Spinner)findViewById(R.id.spinner1); spinnerDistrict = (Spinner)findViewById(R.id.spinner2); stateList = new ArrayList&lt;String&gt;(); districtList = new ArrayList&lt;String&gt;(); </code></pre> <p><strong>Suppose all the data are all ready stored in database.</strong></p> <p>Now I need to retrieve all the 'State' data from database and add it in the statelist which is ArrayList.</p> <pre><code>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(); </code></pre> <p>after this I create one ArrayAdapter and put this state list into this.</p> <pre><code>spinnerState.setAdapter(new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_item, stateList)); </code></pre> <p>Next i put the following code in the activity class:</p> <pre><code>spinnerState.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView&lt;?&gt; parent, View v, int pos, long id) { } @Override public void onNothingSelected(AdapterView&lt;?&gt; arg0) { // TODO Auto-generated method stub } }); </code></pre> <p><strong>Now My problem is here:</strong></p> <ol> <li>How I get the StateId for executing the select query for taking all the district related to particular state in database.</li> <li>How the adapter will generate for District.</li> <li>where I put all these code.</li> </ol> <p>Here what I need creating the districtList after getting the value from state Spinner.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    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