0

I have a raw json file like this

{"_id":707860,"name":"Hurzuf","country":"UA","coord":{"lon":34.283333,"lat":44.549999}}
{"_id":519188,"name":"Novinki","country":"RU","coord":{"lon":37.666668,"lat":55.683334}}
{"_id":1283378,"name":"Gorkhā","country":"NP","coord":{"lon":84.633331,"lat":28}}
{"_id":1270260,"name":"State of Haryāna","country":"IN","coord":{"lon":76,"lat":29}}
{"_id":708546,"name":"Holubynka","country":"UA","coord":{"lon":33.900002,"lat":44.599998}}
{"_id":1283710,"name":"Bāgmatī Zone","country":"NP","coord":{"lon":85.416664,"lat":28}}
{"_id":529334,"name":"Mar’ina Roshcha","country":"RU","coord":{"lon":37.611111,"lat":55.796391}}
{"_id":1269750,"name":"Republic of India","country":"IN","coord":{"lon":77,"lat":20}}
{"_id":1283240,"name":"Kathmandu","country":"NP","coord":{"lon":85.316666,"lat":27.716667}}
{"_id":703363,"name":"Laspi","country":"UA","coord":{"lon":33.733334,"lat":44.416668}}

It is NOT an JSON array - it is a huge list of JSON objects. Here Populate the spinner from JSON In Android, here Convert json array to list of json(raw) strings here android how to convert json array to string array or even here https://developer.android.com/guide/topics/ui/controls/spinner.html it is assumed that I have a JSON array or static data. I'd like to have two spinners in which one is for country and second one is for city. Let's assume I have list of countries so it will be a static data. I'd like the second spinner to dynamically load from raw json file all cities that corresponds to selected city in first spinner. I think that I can handle spinners thing but how can I load data from raw json file into spinner? Do I need to modify this raw file so that it will be a json array and then do something like this:

JSONArray jsonArray = new JSONArray(arraytitle);
List<String> list = new ArrayList<String());
for(int i = 0 ; i < jsonArray.length();i++){
list.add(jsonArray.getJSONObject(i));
}

EDIT I found this answer https://stackoverflow.com/a/26780581/4671908 - if I will succeed I will post an answer

Community
  • 1
  • 1
Colonder
  • 1,556
  • 3
  • 20
  • 40

1 Answers1

2

So you have two questions:

How to update the second Spinner dynamically when the first Spinner is updated

Three thing you need to do:

  1. Implement AdapterView.OnItemSelectedListener
  2. Update the backed data set inside the adapter of the second Spinner. In your case it would be a list of city. (To do this you need to create your own adapter extended from ArrayAdapter)
  3. Call adapter.notifyDataSetChanged()

How to use the JSON

There are several solutions there. But it would be hard for others to provide the one that would suit your case the best without knowing any detail of the requirement.

Just remember one thing when you face design problem:

Do the simplest thing first.

Peike
  • 727
  • 4
  • 15