Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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) {
- }
- }
- {
- "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"
- }]
- }]
- }]
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement