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.
 2. How the adepter 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.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to