PickerView实现三级联动

依赖

implementation 'com.contrarywind:Android-PickerView:4.1.8'

代码


public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    final ArrayList list = new ArrayList<>();
    List list1=new ArrayList<>();
    ArrayList> list2=new ArrayList<>();
    ArrayList>> list3=new ArrayList<>();


    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    StringBuffer stringBuffer = new StringBuffer();
                    final InputStream inputStream = getAssets().open("province.json");
                    byte[] bytes = new byte[1024];
                    int len = 0;
                    while ((len = inputStream.read(bytes) )!= -1){
                        stringBuffer.append(new String(bytes,0,len));
                    }
                    final String s = stringBuffer.toString();
                    list.addAll((Collection) new Gson().fromJson(s, new TypeToken>() {}.getType()));
                    for (int i = 0; i < list.size(); i++) {
                        list1.add(list.get(i).getName());
                        final List city = list.get(i).getCity();
                        ArrayList city_name=new ArrayList<>();
                        List>lists=new ArrayList<>();
                        for (int i1 = 0; i1 < city.size(); i1++) {
                            city_name.add(city.get(i1).getName());
                            lists.add(city.get(i1).getArea());
                        }
                        list2.add(city_name);
                        list3.add(lists);
                    }

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

            }
        }.start();

    }

    public void into(View view) {

        final OptionsPickerView optionsPickerView = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                String tx = list.get(options1).getName()
                        + list.get(options1).getCity().get(options2).getName()
                        + list.get(options1).getCity().get(options2).getArea().get(options3);

                Toast.makeText(MainActivity.this, ""+tx, Toast.LENGTH_SHORT).show();


            }
        }).setOptionsSelectChangeListener(new OnOptionsSelectChangeListener() {
            @Override
            public void onOptionsSelectChanged(int options1, int options2, int options3) {
                String str = "options1: " + options1 + "\noptions2: " + options2 + "\noptions3: " + options3;


            }
        }).setSubmitText("确定")//确定按钮文字
                .setCancelText("取消")//取消按钮文字
                .setTitleText("城市选择")//标题
                .setLabels("省", "市", "区")//设置选择的三级单位
                .isCenterLabel(false) //是否只显示中间选中项的label文字,false则每项item全部都带有label。
                .setOutSideCancelable(false)//点击外部dismiss default true
                .setBgColor(Color.BLACK)
                .build();

        optionsPickerView.setPicker(list1,list2,list3);
        optionsPickerView.show();

    }

    }


需要一个bean类

你可能感兴趣的:(专高三)