1. 程式人生 > >基於github 開源框架Android-PickerView-master實現省市三級聯動

基於github 開源框架Android-PickerView-master實現省市三級聯動

廢話不多說,直接上程式碼。註釋也齊全:

package com.bigkoo.pickerviewdemo;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.OptionsPickerView;
import com.bigkoo.pickerview.TimePickerView;
import com.bigkoo.pickerviewdemo.bean.China;
import com.bigkoo.pickerviewdemo.bean.ProvinceBean;
import com.google.gson.Gson;


public class MainActivity extends Activity {

    private ArrayList<ProvinceBean> options1Items = new ArrayList<ProvinceBean>();
    private ArrayList<ArrayList<String>> options2Items = new ArrayList<ArrayList<String>>();
    private ArrayList<ArrayList<ArrayList<String>>> options3Items = new ArrayList<ArrayList<ArrayList<String>>>();
    private TextView tvOptions;
    TimePickerView pvTime;
    OptionsPickerView pvOptions;
    View vMasker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        vMasker=findViewById(R.id.vMasker);
        tvOptions=(TextView) findViewById(R.id.tvOptions);
        //選項選擇器
        pvOptions = new OptionsPickerView(this);

        //三級聯動效果
        pvOptions.setPicker(options1Items, options2Items, options3Items, true);
        //設定選擇的三級單位
//        pwOptions.setLabels("省", "市", "區");
        pvOptions.setTitle("選擇城市");
        pvOptions.setCyclic(false, true, true);
        //設定預設選中的三級專案
        //監聽確定選擇按鈕
        pvOptions.setSelectOptions(1, 1, 1);
        pvOptions.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {

            @Override
            public void onOptionsSelect(int options1, int option2, int options3) {
                //返回的分別是三個級別的選中位置
                String tx = options1Items.get(options1).getPickerViewText()
                        + options2Items.get(options1).get(option2)
                        + options3Items.get(options1).get(option2).get(options3);
                tvOptions.setText(tx);
                vMasker.setVisibility(View.GONE);
            }
        });
        //點選彈出選項選擇器
        tvOptions.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                pvOptions.show();
            }
        });
    }
    private void initData(){
        try {
			//解析json資料
            InputStream is = getResources().getAssets().open("city.json");

            int available = is.available();

            byte [] b=new byte[available];
            int read = is.read(b);
			//注意格式,utf-8 或者gbk  否則解析出來可能會出現亂碼
            String json=new String(b,"GBK");
     
            System.out.println(json);

            Gson gson= new Gson();
            China china = gson.fromJson(json, China.class);
            ArrayList<China.Province> citylist = china.citylist;
            //======省級
            for (China.Province province: citylist
                    ) {
                String provinceName = province.p;

               // System.out.println("provinceName==="+provinceName);
                ArrayList<China.Province.Area> c = province.c;
                //選項1
                options1Items.add(new ProvinceBean(0,provinceName,"",""));
                ArrayList<ArrayList<String>> options3Items_01 = new ArrayList<ArrayList<String>>();
                //區級
                //選項2

                ArrayList<String> options2Items_01=new ArrayList<String>();
                if (c!=null) {
                    for (China.Province.Area area : c
                            ) {
                        //System.out.println("area------" + area.n + "------");

                        options2Items_01.add(area.n);
                        ArrayList<China.Province.Area.Street> a = area.a;
                        ArrayList<String> options3Items_01_01=new ArrayList<String>();

                        //縣級
                        if (a!=null) {
                            for (China.Province.Area.Street street : a
                                    ) {
                               // System.out.println("street/////" + street.s);
                                options3Items_01_01.add(street.s);
                            }
                            options3Items_01.add(options3Items_01_01);
                        }else{
                            //縣級為空的時候
                            options3Items_01_01.add("");
                            options3Items_01.add(options3Items_01_01);
                        }
                    }
                    options2Items.add(options2Items_01);
                }else{
                    //區級為空的時候  國外
                    options2Items_01.add("");
                }
                options3Items.add(options3Items_01);
                ArrayList<String> options3Items_01_01=new ArrayList<String>();
                options3Items_01_01.add("");
                options3Items_01.add(options3Items_01_01);
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return format.format(date);
    }

}