user1472085
user1472085

Reputation: 1

How to populate spinner from mySql database

How do i populate this,I have 2 Spinners in my app and one is for Province and other is for City, when i select Gauteng in province spinner i want my second spinner to show Johannesburg,Pretoria,Centurion and If i select KZN in province i want my second spinner to show Maritsburg and Durban.

im using mySql database and i have Province table and City table. Here is My Province table(idprovince,provincename)

1   Gauteng 
2   Free-State 
3   Limpopo 
4   Northen-Cape 
5   North-West 
6   Western-Cape 
7   Eastern-Cape 
8   Kwa-Zulu-Natal 
9   Mpumalanga 

and Here is my city table(idcity,cityname,province_idprovince)

1  Johannesburg    1 
2   pretoria    1 
3   Centurion   1 
4   Bloemfontein    2 
5   Welkom  2 
6   Polokwane   3 
7   Phalaborwa  3 
8   Potgiersrus 3 
9   Tzaneen 3 
10  Kimberley   4 
11  Upington    4 
12  Mafikeng    5 
13  Klerksdorp  5 
14  Mmabatho    5 
15  Potchefstroom   5 
16  Brits   5 
17  Cape-Town   6 
18  Stellenbosch    6 
19  George  6 
20  Saldhana-Bay    6 
21  Bisho   7 
22  Port-Elizabeth  7 
23  East-London 7 
24  Pietermaritzburg    8 
25  Durban  8 
26  Ulundi  8 
27  Richards-Bay    8 
28  Newcastle   8 
29  Nelspruit   9 
30  Witbank 9 
31  Middleburg  9 
32  Ermelo  9

i will appriciate your help.

Upvotes: 0

Views: 3732

Answers (3)

user1057197
user1057197

Reputation: 489

here is an example . here i got the details from sever using SAX parsing .here District_districtname and district_districtid are arraylists where i am storing the details from server

Spinner sp1, sp2;
ArrayList<String> loc = new ArrayList<String>();
static ArrayList<String> mov = new ArrayList<String>();

ArrayAdapter<String> ad = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, district_districtname);
    // apply the Adapter:
    sp1.setAdapter(ad);
    sp1.clearFocus();

    sp1.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {

            int pos = sp1.getSelectedItemPosition();
            selecteddistrict = district_districtname.get(pos);

            loc.clear();
            for (int i = 0; i < district_districtname.size(); i++) {

                if (district_districtname.get(i).equals(selecteddistrict)) {

                    districtid = district_districtid.get(i);
                    for (int j = 0; j < locality_localityname.size(); j++) {

                        if (locality_districtid.get(j).equals(districtid)) {

                            loc.add(locality_localityname.get(j));

                        }
                    }
                }
            }


            sp2 = (Spinner) findViewById(R.id.Spinner01);

    ArrayAdapter<String> ad2 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, loc);
    // apply the Adapter:
    sp2.setAdapter(ad2);

    sp2.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {

}

Upvotes: 0

Anu
Anu

Reputation: 7177

ok then you should take a look at some tutorials to get the basic of spinners after doing that

1 step: get the data from db and set it to first spinner using adapter

2 step: on the first spinner's OnItemSelected event you have to set second adapter as well (get data from db according to selected item and set it to second spinner using adapter)

Upvotes: 0

darren
darren

Reputation: 19399

You should take a look at loading your spinners by following some tutorials such as this one http://www.dcpagesapps.com/developer-resources/android/21-android-tutorial-spinners?start=2

Upvotes: 1

Related Questions