我怎样才能从JSON获得国家和州,并在Spinner上填充它

时间:2016-09-09 06:28:50

标签: android json android-studio spinner adapter

我有国家和州的JSON格式,如何在一个[ { "Country": "Afghanistan", "State": [ "Badakhshan", "Badgis", "Baglan", "Balkh", "Bamiyan", "Farah", "Faryab", "Gawr", "Gazni", "Herat", "Hilmand", "Jawzjan", "Kabul", "Kapisa", "Khawst", "Kunar", "Lagman", "Lawghar", "Nangarhar", "Nimruz", "Nuristan", "Paktika", "Paktiya", "Parwan", "Qandahar", "Qunduz", "Samangan", "Sar-e Pul", "Takhar", "Uruzgan", "Wardag", "Zabul" ] }, { "Country": "Albania", "State": [ "Badakhshan", "Badgis", "Baglan", "Balkh", "Bamiyan", "Farah", "Faryab", "Gawr", "Gazni", "Herat", "Hilmand", "Jawzjan", "Kabul", "Kapisa", "Khawst", "Kunar", "Lagman", "Lawghar", "Nangarhar", "Nimruz", "Nuristan", "Paktika", "Paktiya", "Parwan", "Qandahar", "Qunduz", "Samangan", "Sar-e Pul", "Takhar", "Uruzgan", "Wardag", "Zabul", "Berat", "Bulqize", "Delvine", "Devoll", "Dibre", "Durres", "Elbasan", "Fier", "Gjirokaster", "Gramsh", "Has", "Kavaje", "Kolonje", "Korce", "Kruje", "Kucove", "Kukes", "Kurbin", "Lezhe", "Librazhd", "Lushnje", "Mallakaster", "Malsi e Madhe", "Mat", "Mirdite", "Peqin", "Permet", "Pogradec", "Puke", "Sarande", "Shkoder", "Skrapar", "Tepelene", "Tirane", "Tropoje", "Vlore" ] }, 中添加国家/地区并在另一个微调器中获取相应的状态,我的县和州的JSON格式为:< / p>

{{1}}

我搜索了很多资源,但我找不到合适的方法来执行上述方法。通过添加自定义数组中的国家/地区的每个人都可以帮我解决这个问题。

2 个答案:

答案 0 :(得分:3)

您需要执行以下步骤,

  1. 从json获取数据,
  2. 添加国家/地区数组并从json添加数组状态
  3. 现在,OnItemSelected国家/地区位置相应地绑定状态
  4. EX:

    //Setting Adapter for country spinner 
    
            if (stateDataList != null && stateDataList.size() > 0) {
                    StateSpinnerAdapter StateAdapter = new StateSpinnerAdapter(this, stateDataList);
                    SpinState.setAdapter(StateAdapter);
                }
    

    setOnItemSelectedListener国家微调器获取状态

    private ArrayList<ModelStateDetails> stateDataList;
    
            SpinState.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
                    if (position > 0) {
                        new LongOperation().execute("" + position);
                        Log.e(5, "state code", "-->>" + position);
                    }
                }
    
                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {
    
                }
            });
    

答案 1 :(得分:0)

我已经实现了相同的功能,以便找到适当的语言环境。此示例使用3个微调器。您可以根据需要使用。

 //set onItemSelected Listener


           spinProvince.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    String province = (String)parent.getItemAtPosition(position);

                    if(province.matches("Eastern Cape")){
                        spinPro = 1;
                        populateDist();
                    }
                    else if(province.matches("Free State")){
                        spinPro = 2;
                        populateDist();
                    }
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                    Toast.makeText(Reg_Prop.this, "Please select the Province where the Property is located", Toast.LENGTH_LONG).show();

                }
            });

            spinDistrict.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){

                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    String spinDist = (String)parent.getItemAtPosition(position);

                    if(spinDist.matches("Buffalo City") && spinPro == 1){
                        dist = 1;
                        populateLocal();
                    }
                    else if(spinDist.matches("Nelson Mandela Bay") && spinPro == 1){
                        dist = 2;
                        populateLocal();
                    }
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });

   /* Functions to populate the spinners*/

        void populateDist(){
            //Eatern Cape
            if(spinPro == 1){
                String [] ec = {"Select District/Metro *", "Buffalo City", "Nelson Mandela Bay",
                        "Alfred Nzo District", "Amathole District", "Chris Hani District",
                        "Joe Gqabi District", "OR Tambo District", "Cacadu District"};

                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ec);
                spinDistrict.setAdapter(adapter);
            }
            //Free State
            else if(spinPro == 2){
                String [] fs = {"Select District/Metro *", "Mangaung Metropolitan", "Fezile Dabi District",
                        "Lejweleputswa District", "Thabo Mofutsanyana District", "Xhariep District"};

                ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, fs);
                spinDistrict.setAdapter(adapter2);
            }

        }

        void populateLocal(){
            //Baffalo City
            if(dist == 1){
                String [] bc = {"Buffalo City"};

                ArrayAdapter<String> adapterL1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, bc);
                spinLocal.setAdapter(adapterL1);
            }
            //Nelson Mandela Bay
            else if(dist == 2){
                String [] nmb = {"Nelson Mandela Bay"};

                ArrayAdapter<String> adapterL2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, nmb);
                spinLocal.setAdapter(adapterL2);
            }

        }