欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android PickerView实现三级联动效果

程序员文章站 2022-07-04 09:53:30
github有一个开源控件pickerview,可以实现三级联动的效果。虽然该控件使用非常简单,但是填充数据异常繁琐。github上的demo在填充数据的时候是一条一条地填...

github有一个开源控件pickerview,可以实现三级联动的效果。虽然该控件使用非常简单,但是填充数据异常繁琐。github上的demo在填充数据的时候是一条一条地填充的,代码过于冗余。下面提供一种简便的方式来实现。

(1)在app目录下新建一个assets文件夹,将province_data.xml文件复制到该文件夹下,该xml文件里保存了全国的省市县的地址数据。

(2)使用sax解析xml文件中的数据,并将数据填充到pickerview这个控件中。

—provincemodel—

public class provincemodel implements ipickerviewdata
{
  private string name;
  private list<citymodel> citylist;

  public provincemodel()
  {
    super();
  }

  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 getpickerviewtext()
  {
    return name;
  }
}

—citymodel—

public class citymodel implements ipickerviewdata
{
  private string name;
  private list<districtmodel> districtlist;

  public citymodel()
  {
    super();
  }

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

  @override
  public string getpickerviewtext()
  {
    return name;
  }
}

—districtmodel—

public class districtmodel implements ipickerviewdata
{
  private string name;
  private string zipcode;

  public districtmodel()
  {
    super();
  }

  public string getzipcode()
  {
    return zipcode;
  }

  public void setzipcode(string zipcode)
  {
    this.zipcode = zipcode;
  }

  public string getname()
  {
    return name;
  }

  public void setname(string name)
  {
    this.name = name;
  }

  @override
  public string tostring()
  {
    return name;
  }

  @override
  public string getpickerviewtext()
  {
    return name;
  }
}

—xmlparserhandler—

public class xmlparserhandler extends defaulthandler
{
  /**
   *  存储所有的解析对象
   */
  private list<provincemodel> provincelist = new arraylist<>();
  private provincemodel provincemodel = new provincemodel();
  private citymodel citymodel = new citymodel();
  private districtmodel districtmodel = new districtmodel();

  public xmlparserhandler(){}

  public list<provincemodel> getdatalist()
  {
    return provincelist;
  }

  @override
  public void startdocument() throws saxexception
  {
    super.startdocument();
  }

  @override
  public void enddocument() throws saxexception
  {
    super.enddocument();
  }

  @override
  public void startelement(string uri, string localname, string qname, attributes attributes) throws saxexception
  {
    if (qname.equals("province"))
    {
      provincemodel = new 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
  {
    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
  {
    super.characters(ch, start, length);
  }
}

public class addressaddactivity extends baseactivity
{
  private optionspickerview mcitypickerview;
  private arraylist<provincemodel> mprovinces = new arraylist<>();
  private arraylist<arraylist<citymodel>> mcities = new arraylist<>();
  private arraylist<arraylist<arraylist<districtmodel>>> mdistricts = new arraylist<>();

  @override
  protected void oncreate(@nullable bundle savedinstancestate)
  {
    super.oncreate(savedinstancestate);
    supportrequestwindowfeature(window.feature_no_title);
    setcontentview(r.layout.activity_address_add);
    viewutils.inject(this);
    init();
  }

  private void init()
  {
    initprovincedatas();
    try
    {
      //选项选择器
      mcitypickerview = new optionspickerview(this);
      mcitypickerview.setpicker( mprovinces, mcities, mdistricts, true);
      mcitypickerview.settitle("选择城市");
      mcitypickerview.setcyclic(false, false , false);
      mcitypickerview.setonoptionsselectlistener(new optionspickerview.onoptionsselectlistener() {
        @override
        public void onoptionsselect(int options1, int option2, int options3)
        {
          string address = mprovinces.get(options1).getname() + " "
              + mcities.get(options1).get(option2) + " "
              + mdistricts.get(options1).get(option2).get(options3);
          mtxtaddress.settext(address);
        }
      });
    }
    catch (exception e)
    {
      e.printstacktrace();
    }
  }

  @onclick(r.id.ll_city_picker)
  public void showpickerview(view view)
  {
    mcitypickerview.show();
  }

  protected void initprovincedatas()
  {
    assetmanager asset = getassets();
    try
    {
      inputstream input = asset.open("province_data.xml");
      saxparserfactory spf = saxparserfactory.newinstance();
      saxparser parser = spf.newsaxparser();
      xmlparserhandler handler = new xmlparserhandler();
      parser.parse(input , handler);
      input.close();
      //获取解析出来的数据
      mprovinces = (arraylist<provincemodel>) handler.getdatalist();
    }
    catch (throwable e)
    {
      e.printstacktrace();
    }

    if (mprovinces != null)
    {
      for (provincemodel p : mprovinces)
      {
        list<citymodel> cities = p.getcitylist();
        //组装城市数据
        mcities.add((arraylist<citymodel>) cities);
        arraylist<arraylist<districtmodel>> dts = new arraylist<>();
        for (citymodel c : cities)
        {
          list<districtmodel> districts = c.getdistrictlist();
          dts.add((arraylist<districtmodel>) districts);
        }
        mdistricts.add(dts);
      }
    }
  }
}

到此完毕,很简单的例子。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。