Spinner reads data from the database and achieves Level 2 Interaction

Source: Internet
Author: User
This is mainly implemented here. The second-level association between provinces and cities. Select a province. Level 2 corresponds to all cities in the province

See the code below;
1. Activity
@ Override
Protected void oncreate (bundle savedinstancestate ){
// Todo auto-generated method stub
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. test_layout );
This. mcontext = this;
Map <integer, list> DATA = provincedb. getprovince (); // obtain database data
Provincelist = data. Get (1 );
Provincedata = data. Get (0 );

Arrayadapter provinceadapter = new arrayadapter (mcontext, Android. R. layout. simple_spinner_item, provincelist );
Provinceadapter. setdropdownviewresource (Android. R. layout. simple_spinner_dropdown_item); // Style

Spprovince. setadapter (provinceadapter );
Spprovince. setonitemselectedlistener (itemselectedlistener );

}

// Select the province to associate with the target City
Private onitemselectedlistener itemselectedlistener = new onitemselectedlistener (){
@ Override
Public void onitemselected (adapterview <?> Parent, view,
Int position, long ID ){
Spinner spprovince = (spinner) parent;
String selprovince = (string) spprovince. getitematposition (position );
Arrayadapter cityadapter = NULL;
Map <string, integer> DATA = (MAP) provincedata. Get (position); // obtain the corresponding province id based on the selected province ID and then obtain the city
Int pid = data. Get (selprovince); // obtain the province ID
List citylist = provincedb. getcitybypid (PID );
Cityadapter = new arrayadapter (mcontext, Android. R. layout. simple_spinner_item, citylist );
Cityadapter
. Setdropdownviewresource (Android. R. layout. simple_spinner_dropdown_item );
Spcity. setadapter (cityadapter );
Spcity. setonitemselectedlistener (cityselectedlistener );

}

@ Override
Public void onnothingselected (adapterview <?> Parent ){
// Todo auto-generated method stub
}
};

String selectedcity = NULL;
Private onitemselectedlistener cityselectedlistener = new onitemselectedlistener (){
@ Override
Public void onitemselected (adapterview <?> Parent, view,
Int position, long ID ){
Spinner spcity = (spinner) parent;
Selectedcity = (string) spcity. getitematposition (position );
}

@ Override
Public void onnothingselected (adapterview <?> Parent ){
// Todo auto-generated method stub
}
};

2. DB-type query data

Public class provincedb {
Public static Map <integer, list> getprovince (){
String SQL = "select n_provid, s_provname from dict_province ";
Sqlitedatabase DB = NULL;
Cursor c = NULL;
Map <integer, list> provincedata = new hashmap <integer, list> ();
List provincelist = NULL;
Try {
DB = databasehelper. getdatabase ();
C = dB. rawquery (SQL, null );
List provincelist1 = new arraylist ();
List provincelist2 = new arraylist ();
While (C. movetonext ()){
Map provincemap = new hashmap ();
Provincemap. Put (C. getstring (1), C. getint (0 ));
Provincelist1.add (provincemap );
Provincelist2.add (C. getstring (1 ));
}
Provincedata. Put (0, provincelist1 );
Provincedata. Put (1, provincelist2 );
} Catch (exception e ){
E. printstacktrace ();
} Finally {
If (C! = NULL ){
C. Close ();
}
If (DB! = NULL ){
DB. Close ();
}
}
Return provincedata;
}

Public static list <string> getcitybypid (int id ){
String SQL = "select s_cityname from dict_city where n_provid =" + ID;
Sqlitedatabase DB = NULL;
Cursor c = NULL;
List <string> citylist = NULL;
Try {
DB = databasehelper. getdatabase ();
C = dB. rawquery (SQL, null );
Citylist = new arraylist <string> ();
While (C. movetonext ()){
Citylist. Add (C. getstring (0 ));
}
} Catch (exception ex ){
Ex. printstacktrace ();
} Finally {
If (C! = NULL ){
C. Close ();
}
If (DB! = NULL ){
DB. Close ();
}
}
Return citylist;

}
}

3. test_layout.xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"
Android: Orientation = "vertical">

<Linearlayout Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">
<Spinner Android: Id = "@ + ID/sp_bslprovice" Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" Android: layout_weight = "1"/>
<Spinner Android: Id = "@ + ID/sp_bslcity" Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" Android: layout_weight = "1"/>
</Linearlayout>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.