创建另一个ArrayList以在Android中填充微调器

时间:2017-05-26 18:26:35

标签: java android json arraylist

我需要创建另一个带有字符串值的ArrayList来填充状态微调器。初始化应用程序时,状态微调器视图显示空白数据。 Like this SS right here

请遵循以下代码。

public class Activity {

        private Spinner states;
        private Spinner cities;

        ArrayList<String> statesList;
        ArrayList<String> citiesList;

        private void findcomponents() {
            states = (Spinner) findViewById(R.id.spinner_cadastro_estado);
            cities = (Spinner) findViewById(R.id.spinner_cadastro_cidade);

         loadStatesAndCities();

            states.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                      State state = (State) states.getSelectedItem();
                    cities.setAdapter(new ArrayAdapter<>(Activity.this, android.R.layout.simple_spinner_dropdown_item, state.getCities()));
                }

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

                }
            });

            cities.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            }

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

            }
        });
            }

    public void loadStatesAndCities() {
        try {
            JSONObject jsonObject = new JSONObject(loadJSONFromAsset());
            JSONArray statesArray = jsonObject.getJSONArray("estados");

            List<City> citiesList;
            List<State> statesList;

            statesList = new ArrayList<>();
            for (int i = 0; i < statesArray.length(); i++) {
                JSONObject states_object = statesArray.getJSONObject(i);
                String estadoSigla = states_object.optString("sigla");

                JSONArray citiesArray = states_object.getJSONArray("cidades");
                citiesList = new ArrayList<>();

                for (int j = 0; j < citiesArray.length(); j++) {
                    String cities_data = citiesArray.getString(j);
                    citiesList.add(new City(cities_data));

                }
                statesList.add(new State(estadoSigla,citiesList));

            }


            ArrayAdapter<State> stateAdapter;
            stateAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, statesList);
            stateAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            states.setAdapter(stateAdapter);


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

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

0 个答案:

没有答案