I have a problem on Android Spinner. In my application, I created two Spinner on the main layout. 'State' Spinner and 'District' Spinner. All data on Spinner is stored in a SQLite database. I want to show the "District" list on the second counter depending on the choice of a particular "state" in the first counter. Example: suppose when I select Karnataka in the first spinner, then the application first retrieves the entire area from the SQLite database related to the state of Karnataka, and then displays it on the second Spinner.
To do this, I do all the database activities correctly, this means creating two "states" of the table and the "region" in which one column in the environment table is a foreign key that refers to one of the primary key of the "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 data is ready to be stored in a database.
Now I need to get all the "State" data from the database and add it to the article collector, which is an 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 that I create one ArrayAdapter and put this list of states in it.
spinnerState.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, stateList));
Then 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) {
Now my problem is:
- How do I get a StateId to execute a select query so that the entire area is associated with a specific state in the database.
- How the adapter for the district will be generated.
- where i put all this code.
Here is what I need to create theListList after getting the value from the Spinner state.
A similar question that was previously asked on this website is what they are doing: they already create two adapters for two counters, and then use setOnItemSelectedListener. Please help me, because here my mind completely stops working. I send out a lot of books and websites, but do not mention these issues.