基于第二个微调器

时间:2017-01-22 03:46:32

标签: android

我试图根据第一个spinner中的选择来获取第二个微调器中的状态。如果我选​​择印度获得所有状态但我不想那样,我只需要印度国家,请帮助我。我粘贴了我的代码

package com.example.sankar.machinetest;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemSelectedListener {

    private Spinner countrySpinner, stateSpinner, citySpinner;
    private TextView cityCodeTextView;
    private Button submitButton;
    private ArrayList<String> country_list, state_list, city_list;

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

        countrySpinner = (Spinner) findViewById(R.id.county);
        stateSpinner = (Spinner) findViewById(R.id.states);
        citySpinner = (Spinner) findViewById(R.id.city);

        country_list = new ArrayList<String>();
        state_list = new ArrayList<String>();
        city_list = new ArrayList<String>();


        try {
            JSONObject jsonObject = new JSONObject(loadJsonFromAsset());
            JSONArray jsonArray = jsonObject.getJSONArray("countries");

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject countries_object = jsonArray.getJSONObject(i);
                String country_name = countries_object.getString("name");
                String country_code = countries_object.getString("countryCode");
                country_list.add(country_name);

                JSONArray states_array = countries_object.getJSONArray("states");

                for (int j = 0; j < states_array.length(); j++) {
                    JSONObject states_object = states_array.getJSONObject(j);
                    String state_name = states_object.getString("name");
                    state_list.add(state_name);

                    JSONArray city_array = states_object.getJSONArray("cities");
                    for (int k = 0; k < city_array.length(); k++) {
                        JSONObject city_object = city_array.getJSONObject(k);
                        String city_name = city_object.getString("name");
                        String city_code = city_object.getString("code");
                        city_list.add(city_name);

                    }

                }

            }
            ArrayAdapter<String> country_adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, country_list);
            countrySpinner.setAdapter(country_adapter);

            ArrayAdapter<String> city_adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, city_list);
            citySpinner.setAdapter(city_adapter);
            countrySpinner.setOnItemSelectedListener(this);



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


    }



    public String loadJsonFromAsset() {
        String json = null;
        try {
            InputStream is = getAssets().open("countries.json");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            json = new String(buffer, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return json;
    }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {

            if (position==0) {

                ArrayAdapter<String> state_adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, state_list);
                stateSpinner.setAdapter(state_adapter);


            }

    }



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

    }
}

我的json是

    {
  "countries": [{
    "name": "India",
    "countryCode": "91",
    "states": [{
      "name": "Kerala",
      "cities": [{
        "name": "Thrissur",
        "code": "680111"
      }, {
        "name": "Kochi",
        "code": "680222"
      }, {
        "name": "Trivandrum",
        "code": "680333"
      }]
    }, {
      "name": "TamilNadu",
      "cities": [{
        "name": "Chennai",
        "code": "380111"
      }, {
        "name": "Coimbatore",
        "code": "380222"
      }]
    }]
  }, {
    "name": "Germany",
    "countryCode": "49",
    "states": [{
      "name": "Bayern",
      "cities": [{
        "name": "Nurnburg",
        "code": "123"
      }, {
        "name": "Munich",
        "code": "125"
      }]
    }, {
      "name": "Berlin",
      "cities": [{
        "name": "Berlin",
        "code": "223"
      }]
    }]
  }]
}

2 个答案:

答案 0 :(得分:1)

你可以试试这个

public class Country {

    private String name;
    private String code;
    private List<State> states;

    public Country(String name, String code, List<State> states) {
        this.name = name;
        this.code = code;
        this.states = states;
    }

   public String getName() {
        return name;
   }

    public String getCode() {
        return code;
    }

    public List<State> getStates() {
        return states;
    }

    @Override
    public String toString() {
        return name;
    }
}

public class State {

    private String name;
    private List<City> cities;

    public State(String name, List<City> cities) {
        this.name = name;
        this.cities = cities;
    }

    public String getName() {
        return name;
    }

    public List<City> getCities() {
        return cities;
    }

    @Override
    public String toString() {
        return name;
    }
}

public class City {

    private String name;
    private String code;

    public City(String name, String code) {
        this.name = name;
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public String getCode() {
        return code;
    }

    @Override
    public String toString() {
        return name;
    }
}

然后像这样

从JSON构建数据
try {
        JSONObject jsonObject = new JSONObject(loadJsonFromAsset());
        JSONArray jsonArray = jsonObject.getJSONArray("countries");

        List<Country> countries;
        List<City> cities;
        List<State> states;

        countries = new ArrayList<>();
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject countries_object = jsonArray.getJSONObject(i);
            // get country name and code
            String country_name = countries_object.getString("name");
            String country_code = countries_object.getString("countryCode");

            // get country states
            JSONArray states_array = countries_object.getJSONArray("states");
            states = new ArrayList<>();

            for (int j = 0; j < states_array.length(); j++) {
                JSONObject states_object = states_array.getJSONObject(j);
                // get state name
                String state_name = states_object.getString("name");
                // get all city for this state
                JSONArray city_array = states_object.getJSONArray("cities");
                cities = new ArrayList<>();
                for (int k = 0; k < city_array.length(); k++) {
                    JSONObject city_object = city_array.getJSONObject(k);
                    // get city name and code
                    String city_name = city_object.getString("name");
                    String city_code = city_object.getString("code");
                    // add new city
                    cities.add(new City(city_name, city_code));
                }
                // add new state with cities
                states.add(new State(state_name, cities));
            }
            countries.add(new Country(country_name, country_code, states));
        }

        ArrayAdapter<Country> country_adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, countries);
        country_adapter.setAdapter(mCountryAdapter);
        country_adapter.setOnItemSelectedListener(this);

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

最后

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
    Country country = (Country) mCountrySpinner.getSelectedItem();
    mStateSpinner.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, country.getStates()));
}

答案 1 :(得分:0)

String countryCode = null;

You can call your second spinner in onItemSelected method.

countryCode = countrySpinner.getSelectedItem().toString();

// call your second adapter 

before populate state list

HashMap<String, String> hmap = new HashMap<String, String>();
hmap.put(country_code, stateName);