Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. package com.example.sankar.machinetest;
  2. import android.content.res.AssetManager;
  3. import android.content.res.Resources;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.widget.AdapterView;
  9. import android.widget.ArrayAdapter;
  10. import android.widget.Button;
  11. import android.widget.Spinner;
  12. import android.widget.TextView;
  13.  
  14. import org.json.JSONArray;
  15. import org.json.JSONException;
  16. import org.json.JSONObject;
  17.  
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.UnsupportedEncodingException;
  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.Scanner;
  24.  
  25. public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemSelectedListener {
  26.  
  27. private Spinner countrySpinner, stateSpinner, citySpinner;
  28. private TextView cityCodeTextView;
  29. private Button submitButton;
  30. private ArrayList<String> country_list, state_list, city_list;
  31.  
  32. @Override
  33. protected void onCreate(Bundle savedInstanceState) {
  34. super.onCreate(savedInstanceState);
  35. setContentView(R.layout.activity_main);
  36.  
  37. countrySpinner = (Spinner) findViewById(R.id.county);
  38. stateSpinner = (Spinner) findViewById(R.id.states);
  39. citySpinner = (Spinner) findViewById(R.id.city);
  40.  
  41. country_list = new ArrayList<String>();
  42. state_list = new ArrayList<String>();
  43. city_list = new ArrayList<String>();
  44.  
  45.  
  46. try {
  47. JSONObject jsonObject = new JSONObject(loadJsonFromAsset());
  48. JSONArray jsonArray = jsonObject.getJSONArray("countries");
  49.  
  50. for (int i = 0; i < jsonArray.length(); i++) {
  51. JSONObject countries_object = jsonArray.getJSONObject(i);
  52. String country_name = countries_object.getString("name");
  53. String country_code = countries_object.getString("countryCode");
  54. country_list.add(country_name);
  55.  
  56. JSONArray states_array = countries_object.getJSONArray("states");
  57.  
  58. for (int j = 0; j < states_array.length(); j++) {
  59. JSONObject states_object = states_array.getJSONObject(j);
  60. String state_name = states_object.getString("name");
  61. state_list.add(state_name);
  62.  
  63. JSONArray city_array = states_object.getJSONArray("cities");
  64. for (int k = 0; k < city_array.length(); k++) {
  65. JSONObject city_object = city_array.getJSONObject(k);
  66. String city_name = city_object.getString("name");
  67. String city_code = city_object.getString("code");
  68. city_list.add(city_name);
  69.  
  70. }
  71.  
  72. }
  73.  
  74. }
  75. ArrayAdapter<String> country_adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, country_list);
  76. countrySpinner.setAdapter(country_adapter);
  77.  
  78. ArrayAdapter<String> city_adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, city_list);
  79. citySpinner.setAdapter(city_adapter);
  80. countrySpinner.setOnItemSelectedListener(this);
  81.  
  82.  
  83.  
  84. } catch (JSONException e) {
  85. e.printStackTrace();
  86. }
  87.  
  88.  
  89. }
  90.  
  91.  
  92.  
  93. public String loadJsonFromAsset() {
  94. String json = null;
  95. try {
  96. InputStream is = getAssets().open("countries.json");
  97. int size = is.available();
  98. byte[] buffer = new byte[size];
  99. is.read(buffer);
  100. is.close();
  101. json = new String(buffer, "UTF-8");
  102. } catch (UnsupportedEncodingException e) {
  103. e.printStackTrace();
  104. } catch (IOException e) {
  105. e.printStackTrace();
  106. }
  107. return json;
  108. }
  109.  
  110.  
  111. @Override
  112. public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
  113.  
  114. }
  115.  
  116. @Override
  117. public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
  118.  
  119. if (position==0) {
  120.  
  121. ArrayAdapter<String> state_adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, state_list);
  122. stateSpinner.setAdapter(state_adapter);
  123.  
  124.  
  125. }
  126.  
  127. }
  128.  
  129.  
  130.  
  131. @Override
  132. public void onNothingSelected(AdapterView<?> adapterView) {
  133.  
  134. }
  135. }
  136.  
  137. {
  138. "countries": [{
  139. "name": "India",
  140. "countryCode": "91",
  141. "states": [{
  142. "name": "Kerala",
  143. "cities": [{
  144. "name": "Thrissur",
  145. "code": "680111"
  146. }, {
  147. "name": "Kochi",
  148. "code": "680222"
  149. }, {
  150. "name": "Trivandrum",
  151. "code": "680333"
  152. }]
  153. }, {
  154. "name": "TamilNadu",
  155. "cities": [{
  156. "name": "Chennai",
  157. "code": "380111"
  158. }, {
  159. "name": "Coimbatore",
  160. "code": "380222"
  161. }]
  162. }]
  163. }, {
  164. "name": "Germany",
  165. "countryCode": "49",
  166. "states": [{
  167. "name": "Bayern",
  168. "cities": [{
  169. "name": "Nurnburg",
  170. "code": "123"
  171. }, {
  172. "name": "Munich",
  173. "code": "125"
  174. }]
  175. }, {
  176. "name": "Berlin",
  177. "cities": [{
  178. "name": "Berlin",
  179. "code": "223"
  180. }]
  181. }]
  182. }]
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement