Provincial urban three-level linkage, using the SQLite local database, you need to use the Simplecursoradapter class to cooperate with the Drop-down list spinner implementation. For a combination of spinner and simplecursoradapter, see,
Android uses Simplecursoradapter to populate the Drop-down list spinner usage
Put the spinner and Simplecursoradapter consolidated code First:
The code is as follows |
Copy Code |
Cursor C = getcontentresolver (). query (Table.Region.CONTENT_URI, NULL, table.region.parent_id+ "=" +parentregionid, NULL, TABLE.REGION._ID + "ASC"); Final string[] columns = new string[] {Table.Region.REGION_NAME}; Final int[] to = new int[] {Android. R.ID.TEXT1}; Simplecursoradapter madapter = new Simplecursoradapter (this, Android. R.layout.simple_spinner_item, C, columns, to); Madapter.setdropdownviewresource (Android. R.layout.simple_spinner_dropdown_item); View.setadapter (Madapter); |
Let's take a look at the three-level linkage effect in the provincial urban areas
Three-level linkage realization principle: The user selects the province, according to the province ID load corresponding city list, because spinner chooses the first item by default, namely, after the city list loads, then will load the corresponding region list information.
Implement level three linkage using the spinner Onitemselectedlistener Listener class:
The code is as follows |
Copy Code |
Load Provinces Updateregioninfo (spinner_province, 1); Spinner_province.setonitemselectedlistener (New Onitemselectedlistener () { @Override public void onitemselected (adapterview<?> parent, view view, int position, long id) { // Load Next Level (city) Cursor selectedcursor = (Cursor) parent.getselecteditem (); Updateregioninfo (spinner_city, Cursorutil.getint (Selectedcursor, Table.Region.REGION_ID)); } @Override public void onnothingselected (adapterview<?> parent) { } }); Spinner_city.setonitemselectedlistener (New Onitemselectedlistener () { @Override public void onitemselected (adapterview<?> parent, view view, int position, long id) { // Load Next Level (region) Cursor selectedcursor = (Cursor) parent.getselecteditem (); Updateregioninfo (Spinner_district, Cursorutil.getint (Selectedcursor, Table.Region.REGION_ID)); } @Override public void onnothingselected ( Adapterview<?> parent) { } }); |
Use Simplecursoradapter to bind SQLite database to Spinner
The code is as follows |
Copy Code |
private void Updateregioninfo (Spinner view, int parentregionid) { final listadapter adapte R = (listadapter) view.getadapter (); if (Null!= adapter && Adapter instanceof cursoradapter) { & nbsp; final CursorAdapter CursorAdapter = (cursoradapter) adapter; cursoradapter.changecursor (null); view.setadapter (null); } Cursor c = getcontentresolver (). query (Table.Region.CONTENT_URI, NULL, table.region.parent_id+ "=" +parentregionid, NULL, TABLE.REGION._ID + "ASC"); Final string[] columns = new string[] {Table.Region.REGION_NAME}; Final int[] to = new int[] {Android. R.ID.TEXT1}; simplecursoradapter madapter = new Simplecursoradapter (this, Android. R.layout.simple_spinner_item, C, columns, to); Madapter.setdropdownviewresource (android. R.layout.simple_spinner_dropdown_item); View.setadapter (madapter); } |