Android-wheel control realizes three-level linkage effect _android

Source: Internet
Author: User

This example for you to share the Android wheel provinces and cities three-level linkage effect for your reference, the specific content as follows

On the GitHub, there is an open source control called Android-wheel, code address: Https://github.com/maarek/android-wheel


SOURCE Download Address: Http://xiazai.jb51.net/201610/yuanma/AndroidCascadeMaster (jb51.net). rar

Main interface Layout

Activity_main.xml

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http:// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Wrap_content "android:backg" 
    Round= "#E9E9E9" android:orientation= "vertical" > <linearlayout android:layout_width= "fill_parent" 
      android:layout_height= "wrap_content" android:orientation= "Horizontal" > <kankan.wheel.widget.wheelview Android:id= "@+id/id_province" android:layout_width= "0DP" android:layout_height= "Wrap_content" a 
      ndroid:layout_weight= "1" > </kankan.wheel.widget.WheelView> <kankan.wheel.widget.wheelview Android:id= "@+id/id_city" android:layout_width= "0DP" android:layout_height= "Wrap_content" Android:lay out_weight= "1" > </kankan.wheel.widget.WheelView> <kankan.wheel.widget.wheelview android:id = "@+id/id_district" Android:layout_widtH= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" > </kankan.wheel.widget.wh eelview> </LinearLayout> <button android:id= "@+id/btn_confirm" android:layout_width= "Match_p" 
    Arent "android:layout_height=" wrap_content "android:layout_margintop=" 10dip "android:gravity=" center " 
 android:text= "OK" android:textcolor= "#000000"/> </LinearLayout>

Assets resource File--province_data.xml (part of)

<root> 
 <province name= "Anhui Province" > 
  <city name= "Anqing" > 
   <district name= "Congyang County" zipcode= " 246000 "/> <district name=" Daguan District "zipcode=" 246000 "/> <district name=" Huaining County "zipcode=" 246000 "/> 
   <district name= "Qianshan County" zipcode= "246000"/> <district name= 
   "Susong County" zipcode= "246000"/> 
   < District name= "Taihu County" zipcode= "246000"/> <district name= 
   "Tongcheng" zipcode= "246000"/> 
   <district "Wangjiang County" zipcode= "246000"/> 
   <district name= "Yi Xiu qu" zipcode= "246000"/> <district name= "Yinjiang District" ZipCode 
   = "246000"/> 
   <district name= "Yuexi County" zipcode= "246000"/> "<district name=" " 
   Other" zipcode= "246000"/ > 
  </city> 

First look at the provinces, cities, counties and villages Bean class
Provincemodel

Package Com.mrwujay.cascade.model; 
 
Import java.util.List; 
 
public class Provincemodel { 
  private String name; 
  Private list<citymodel> citylist; 
   
  Public Provincemodel () { 
    super (); 
  } 
 
  Public Provincemodel (String name, list<citymodel> citylist) { 
    super (); 
    this.name = name; 
    This.citylist = citylist; 
  } 
 
  Public String GetName () {return 
    name; 
  } 
 
  public void SetName (String name) { 
    this.name = name; 
  } 
 
  Public list<citymodel> getcitylist () {return 
    citylist; 
  } 
 
  public void Setcitylist (list<citymodel> citylist) { 
    this.citylist = citylist; 
  } 
 
  @Override public 
  String toString () {return 
    "Provincemodel [name=" + name + ", citylist=" + CityList + "]"; 
   
    } 
   
} 

   

Package Com.mrwujay.cascade.model; 
 
Import java.util.List; 
 
public class Citymodel { 
  private String name; 
  Private list<districtmodel> districtlist; 
   
  Public Citymodel () { 
    super (); 
  } 
 
  Public Citymodel (String name, list<districtmodel> districtlist) { 
    super (); 
    this.name = name; 
    This.districtlist = districtlist; 
  } 
 
  Public String GetName () {return 
    name; 
  } 
 
  public void SetName (String name) { 
    this.name = name; 
  } 
 
  Public list<districtmodel> getdistrictlist () {return 
    districtlist; 
  } 
 
  public void Setdistrictlist (list<districtmodel> districtlist) { 
    this.districtlist = districtlist; 
  } 
 
  @Override public 
  String toString () {return 
    "Citymodel [name=" + name + ", districtlist=" + districtlist 
        + "]"; 
  } 
   
} 

Districtmodel

Package Com.mrwujay.cascade.model; 
 
public class Districtmodel { 
  private String name; 
  Private String zipcode; 
   
  Public Districtmodel () { 
    super (); 
  } 
 
  Public Districtmodel (string name, String zipcode) { 
    super (); 
    this.name = name; 
    This.zipcode = ZipCode; 
  } 
 
  Public String GetName () {return 
    name; 
  } 
 
  public void SetName (String name) { 
    this.name = name; 
  } 
 
  Public String Getzipcode () {return 
    zipcode; 
  } 
 
  public void Setzipcode (String zipcode) { 
    this.zipcode = zipcode; 
  } 
 
  @Override public 
  String toString () {return 
    "Districtmodel [name=" + name + ", zipcode=" + ZipCode + "]"; 
  } 
 
} 

Look at the custom Xmlparserhandler

Package com.mrwujay.cascade.service; 
Import java.util.ArrayList; 
Import java.util.List; 
Import org.xml.sax.Attributes; 
Import org.xml.sax.SAXException; 
 
Import Org.xml.sax.helpers.DefaultHandler; 
Import Com.mrwujay.cascade.model.CityModel; 
Import Com.mrwujay.cascade.model.DistrictModel; 
 
Import Com.mrwujay.cascade.model.ProvinceModel; public class Xmlparserhandler extends DefaultHandler {/** * stores all resolved objects/private List<provincemodel&gt ; 
 
  Provincelist = new arraylist<provincemodel> (); 
    Public Xmlparserhandler () {}/** * External exposure method * * Public list<provincemodel> getdatalist () { 
  return provincelist; @Override public void Startdocument () throws Saxexception {//When the first start tag is read, this method is triggered} Provincemode 
  L Provincemodel = new Provincemodel (); 
  Citymodel Citymodel = new Citymodel (); 
Districtmodel Districtmodel = new Districtmodel (); /** * <province name= "Anhui Province" > <city name= "Anqing" > 
   <district name= "Congyang County" zipcode= "246000"/> <district name= "Daguan District" zipcode= "246000"/> <district na Me= "Huaining County" zipcode= "246000"/> <district name= "Qianshan County" zipcode= "246000"/> <district name= "Susong County" zipcode= "24 6000 "/> <district name=" Taihu County "zipcode=" 246000 "/> <district name=" Tongcheng "zipcode=" 246000 "/> < District name= "Wangjiang County" zipcode= "246000"/> <district "Yi Xiu qu" name= "zipcode=" 246000/> <district "Name=" Zipcode= "246000"/> <district name= "Yuexi County" zipcode= "246000"/> <district name= "Other" zipcode= "246000"/&gt 
  ; </city> * @Override public void startelement (string uri, String localname, String qName, Attributes attributes) throws Saxexception {//When the start tag is encountered, call this method if (Qname.equals ("province")) {Provincemodel = NE 
      W Provincemodel (); 
      Provincemodel.setname (attributes.getvalue (0)); Provincemodel.setcitylist (New arraylist<citymodel> ());
    else if (qname.equals ("City")) {Citymodel = new Citymodel (); 
      Citymodel.setname (attributes.getvalue (0)); 
    Citymodel.setdistrictlist (New arraylist<districtmodel> ()); 
      else if (qname.equals ("district")) {Districtmodel = new Districtmodel (); 
      Districtmodel.setname (attributes.getvalue (0)); 
    Districtmodel.setzipcode (Attributes.getvalue (1)); 
    @Override public void EndElement (string uri, String localname, String qName) throws Saxexception { 
    When an end tag is encountered, this method is called if (Qname.equals ("district")) {citymodel.getdistrictlist (). Add (Districtmodel); 
    else if (qname.equals ("City")) {provincemodel.getcitylist (). Add (Citymodel); 
    else if (qname.equals ("province")) {Provincelist.add (Provincemodel); 
 @Override public void characters (char[] ch, int start, int length) throws Saxexception {}}

Next look at the base class Baseactivity

Package com.mrwujay.cascade.activity; 
Import Java.io.InputStream; 
Import Java.util.HashMap; 
Import java.util.List; 
Import Java.util.Map; 
Import Javax.xml.parsers.SAXParser; 
Import Javax.xml.parsers.SAXParserFactory; 
Import android.app.Activity; 
 
Import Android.content.res.AssetManager; 
Import Com.mrwujay.cascade.model.CityModel; 
Import Com.mrwujay.cascade.model.DistrictModel; 
Import Com.mrwujay.cascade.model.ProvinceModel; 
 
Import Com.mrwujay.cascade.service.XmlParserHandler; 
  public class Baseactivity extends activity {/** * all provinces * * protected string[] Mprovincedatas; /** * Key-province Value-City * * protected map<string, string[]> mcitisdatasmap = new hashmap<string, String 
  []> (); /** * Key-City Values-District * * Protected map<string, string[]> mdistrictdatasmap = new hashmap<string, St 
 
  Ring[]> (); /** * Key-zone values-zip/protected map<string, string> mzipcodedatasmap = new Hashmap<strING, string> (); 
  /** * Current Province name */protected String mcurrentprovicename; 
  /** * The name of the current city/protected String mcurrentcityname; 
 
  /** * The name of the current zone */protected String mcurrentdistrictname = ""; 
 
  /** * Current Area ZIP/protected String mcurrentzipcode = ""; /** * Analysis of the provincial urban XML data */protected void Initprovincedatas () {//Province collection list list<provincemodel> Provin 
    Celist = null; 
    Get resources Assetmanager asset = Getassets (); 
      try {//Get input stream InputStream input = Asset.open ("Province_data.xml"); 
      Create a factory object that parses xml saxparserfactory SPF = saxparserfactory.newinstance (); 
      Parse XML saxparser parser = Spf.newsaxparser (); 
      Parsing tool Xmlparserhandler handler = new Xmlparserhandler (); 
      Parsing parser.parse (input, handler); 
      Input.close (); 
      Gets the parsed data Provincelist = Handler.getdatalist (); * * Initialize the default selected province, city, or district if (provincelist!= null &Amp;&!provincelist.isempty ()) {//get first province mcurrentprovicename = Provincelist.get (0). GetName (); 
        List<citymodel> citylist = provincelist.get (0). Getcitylist (); if (citylist!= null &&!citylist.isempty ()) {//Get first city name of First province mcurrentcityname = CityList. 
          Get (0). GetName (); 
          list<districtmodel> districtlist = citylist.get (0). Getdistrictlist (); 
          Get first county of first city name mcurrentdistrictname = Districtlist.get (0). GetName (); 
        Mcurrentzipcode = Districtlist.get (0). Getzipcode (); 
      }///* Mprovincedatas = new string[provincelist.size ()]; for (int i = 0; i < provincelist.size (); i++) {//Traverse all provincial data mprovincedatas[i] = Provincelist.get (i). g 
         
        Etname (); 
        List<citymodel> citylist = Provincelist.get (i). Getcitylist (); 
        string[] Citynames = new string[citylist.size ()]; for (int j = 0; J < Citylist.size (); 
          J + +) {//Traverse data citynames[j of all cities below province] = Citylist.get (j). GetName (); 
          list<districtmodel> districtlist = Citylist.get (j). Getdistrictlist (); 
          string[] Distrinctnamearray = new string[districtlist. Size ()]; 
          districtmodel[] Distrinctarray = new districtmodel[districtlist. Size ()]; for (int k = 0; k < districtlist.size (); k++) {//Traverse all districts/counties below the city Districtmodel Districtmodel = New Districtmodel (Districtlist.get (k). GetName (), districtlist. Get (k). Getzipcode ( 
            )); District/County for the ZIP code, save to Mzipcodedatasmap Mzipcodedatasmap.put (Districtlist.get (k). GetName (), District 
            List.get (k). Getzipcode ()); 
            Distrinctarray[k] = Districtmodel; 
          Distrinctnamearray[k] = Districtmodel.getname (); }//city-district/County data, save to Mdistrictdatasmap
          Mdistrictdatasmap.put (Citynames[j], distrinctnamearray); 
      }//province-city data, saved to Mcitisdatasmap Mcitisdatasmap.put (Provincelist.get (i). GetName (), citynames); 
    } catch (Throwable e) {e.printstacktrace (); 
 } finally {}}}

Main interface mainactivity

Package com.mrwujay.cascade.activity; 
Import COM.MRWUJAY.CASCADE.R; 
Import com.mrwujay.cascade.r.id; 
 
Import Com.mrwujay.cascade.r.layout; 
Import Kankan.wheel.widget.OnWheelChangedListener; 
Import Kankan.wheel.widget.WheelView; 
Import Kankan.wheel.widget.adapters.ArrayWheelAdapter; 
Import Android.os.Bundle; 
Import android.app.Activity; 
Import Android.view.Menu; 
Import Android.view.View; 
Import Android.view.View.OnClickListener; 
Import Android.widget.Button; 
 
Import Android.widget.Toast; public class Mainactivity extends Baseactivity implements Onclicklistener, Onwheelchangedlistener {private Wheelview M 
  Viewprovince; 
  Private Wheelview mviewcity; 
  Private Wheelview mviewdistrict; 
 
  Private Button mbtnconfirm; 
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
    Setcontentview (R.layout.activity_main); 
    Setupviews (); 
    Setuplistener (); 
  SetupData (); private void Setupviews (){mviewprovince = (Wheelview) Findviewbyid (r.id.id_province); 
    Mviewcity = (Wheelview) Findviewbyid (r.id.id_city); 
    Mviewdistrict = (Wheelview) Findviewbyid (r.id.id_district); 
  Mbtnconfirm = (Button) Findviewbyid (r.id.btn_confirm); 
    private void Setuplistener () {//Add Change Event Mviewprovince.addchanginglistener (this); 
    Add Change Event Mviewcity.addchanginglistener (this); 
    Add Change Event Mviewdistrict.addchanginglistener (this); 
  Add onclick event Mbtnconfirm.setonclicklistener (this); 
    private void SetupData () {Initprovincedatas (); 
    Mviewprovince.setviewadapter (New arraywheeladapter<string> (Mainactivity.this, MProvinceDatas)); 
    Set the number of visible entries Mviewprovince.setvisibleitems (7); 
    Mviewcity.setvisibleitems (7); 
    Mviewdistrict.setvisibleitems (7); 
    Updatecities (); 
  Updateareas (); 
    /** * According to the current province, update the information of the city Wheelview * * private void Updatecities () {//Get the current province's Itme index valueint pcurrent = Mviewprovince.getcurrentitem (); 
    Lake District Current province Name mcurrentprovicename = Mprovincedatas[pcurrent]; 
    Get the following city array set string[] cities = mcitisdatasmap.get (Mcurrentprovicename); 
    if (cities = = null) {cities = new string[] {"}; 
    } mviewcity.setviewadapter (this, cities) (new arraywheeladapter<string>); 
    Mviewcity.setcurrentitem (0); 
  Updateareas (); /** * According to the current city, the new zone wheelview information * * private void Updateareas () {int pcurrent = Mviewcity.getcurrentitem ( 
    ); 
    Mcurrentcityname = Mcitisdatasmap.get (Mcurrentprovicename) [pcurrent]; 
 
    string[] areas = Mdistrictdatasmap.get (Mcurrentcityname); 
    if (areas = = null) {areas = new string[] {"}; 
    } mviewdistrict.setviewadapter (this, areas) (new arraywheeladapter<string>); 
  Mviewdistrict.setcurrentitem (0); /** * Callback to Implement interface method/@Override public void onchanged (Wheelview wheel, int oldValue, int newvalu 
 E) {   TODO auto-generated Method Stub if (wheel = = mviewprovince) {updatecities (); 
    else if (wheel = = mviewcity) {updateareas (); else if (wheel = = mviewdistrict) {//get county name Mcurrentdistrictname = Mdistrictdatasmap.get (mcurrentcityname) 
      [NewValue]; 
    Gets the coding of the county Mcurrentzipcode = Mzipcodedatasmap.get (mcurrentdistrictname); @Override public void OnClick (View v) {switch (V.getid ()) {case R.id.btn_confirm:sh 
      Owselectedresult (); 
    Break 
    Default:break; The private void Showselectedresult () {Toast.maketext (Mainactivity.this) is currently selected: "+mcurrentprovicename+", "+mcu 
  Rrentcityname+ "," +mcurrentdistrictname+ "," +mcurrentzipcode, Toast.length_short ". Show (); 
 } 
}

There are 2 more drawable.
Wheel_bg.xml

<?xml version= "1.0" encoding= "Utf-8"?> <layer-list xmlns:android= 
 
 
"Http://schemas.android.com/apk/res" /android "> 
   
</layer-list> 

Wheel_val.xml

<?xml version= "1.0" encoding= "Utf-8"?> <shape xmlns:android= 
"http://schemas.android.com/apk/res/" Android "> 
 
  <gradient 
    android:angle=" 
    android:centercolor= "#70222222" 
    android:endcolor= "#70EEEEEE" 
    android:startcolor= "#70222222"/> 
 
  <stroke 
    android:width= "20DP" 
    android: Color= "#FF69B4"/> 
 
</shape> 

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.