I have 3 spinners and I want to parse JSON data into them. For example the JSON file structure has Converterno, zoneno and codeid . So i want to populate all the converter no. in one spinner and zoneno. in another spinner and similarly the 3rd spinner. Iam trying this in android and trying to parse data from json. Please help. My xml file is fine, just need help in java class to get the values to dropdown
Here is the latest code (Selectcode.java) i did with the help of @Lucefer
package com.example.com;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class selectcode extends AppCompatActivity {
String URL = "*url_to_json_php*";
List<String> converter = new ArrayList<>();
List<String> zone = new ArrayList<>();
List<String> codeid = new ArrayList<>();
JSONObject jsonObject;
JSONArray jsonArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selectcode);
try{
jsonObject=new JSONObject(URL);
jsonArray=jsonObject.getJSONArray("server_response");
for (int i=0;i<jsonArray.length();i++){
jsonObject=jsonArray.getJSONObject(i);
converter.add(jsonObject.optString("converternumber"));
zone.add(jsonObject.optString("zonenumber"));
codeid.add(jsonObject.optString("codeid"));
}
} catch (JSONException e) {
e.printStackTrace();
}
Spinner spinner1=(Spinner) findViewById(R.id.selectconverter);
ArrayAdapter<String> adapter1=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
Spinner spinner2=(Spinner) findViewById(R.id.selectzone);
ArrayAdapter<String> adapter2=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
Spinner spinner3=(Spinner) findViewById(R.id.selectcode);
ArrayAdapter<String> adapter3=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item);
spinner3.setAdapter(adapter3);
}
}
I got the solution at