Android spinner realizes three-level linkage program code in provincial urban area

Source: Internet
Author: User
Tags sqlite sqlite database

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);
}

Related Article

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.