API spinner populate, how can i get id from json file to spinner text? I need it to populate second spinner

16 Views Asked by At

I have a easy student project, so the goal is get from api regions, country and city to spinners, i populated a region spinner, so i can getSelectedItemPosition to have id of region (in json regions have id's like 1,2,3,4 so its working), but i have 2 problems, when i switch a region (spinner item for ex: africa to asia) my country spinner populating and i have a list of country's from africa and asia, not only from asia and so the question is how can i set id from json to spinner item or somth like that, i need it for populate third spinner with cities, like if country id = 2 then get json response with parameter country = 2 and populate spinner from response (sry for my eng, still learning). Probably i need to create something like dictionary and populate it with id and string?

** MainActivity code**

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button btnExplore;
    Spinner regionSpinner, countrySpinner, citySpinner;
    private String URLReg = "http://194.87.68.149:5011/rpc/get_regions";
    private String URLCountry = "http://194.87.68.149:5011/rpc/get_countries?region=";
    private String URLCity = "http://194.87.68.149:5011/rpc/get_cities?country=2";
    int regionid = 0;

    private ArrayList<GoodModel> goodModelArrayList;
    private ArrayList<CityClass> countryArrayList;
    private ArrayList<String> regions = new ArrayList<String>();
    private ArrayList<String> countrys = new ArrayList<String>();
    private ArrayList<String> citys = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        regionSpinner = (Spinner) findViewById((R.id.spReg));
        countrySpinner = (Spinner) findViewById((R.id.spCountry));
        citySpinner = (Spinner) findViewById((R.id.spCity));
        btnExplore = (Button) findViewById(R.id.btnExplore);
        btnExplore.setOnClickListener((View.OnClickListener) this);

        regionid = 1;
        retrieveJSONRegions();

        regionSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
               regionid += regionSpinner.getSelectedItemPosition();
               URLCountry = URLCountry + regionid;
               retrieveJSONCountry();
               URLCountry = "http://194.87.68.149:5011/rpc/get_countries?region=";
            }

            public void onNothingSelected(AdapterView<?> adapterView) {
                return;
            }
        });
    }

    private void retrieveJSONRegions() {
        StringRequest stringRequest = new StringRequest(Request.Method.GET, URLReg,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONArray obj = new JSONArray(response);
                            if(1==1){
                                goodModelArrayList = new ArrayList<>();
                                for (int i = 0; i < obj.length(); i++) {
                                    GoodModel playerModel = new GoodModel();
                                    JSONObject dataobj = obj.getJSONObject(i);
                                    playerModel.setIdentifier(dataobj.getInt("identifier"));
                                    playerModel.setDescription(dataobj.getString("description"));
                                    goodModelArrayList.add(playerModel);
                                }

                                for (int i = 0; i < goodModelArrayList.size(); i++){
                                    regions.add(goodModelArrayList.get(i).getDescription().toString());
                                    Log.d("id", goodModelArrayList.get(i).getIdentifier() );
                                }
                                ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(MainActivity.this, simple_spinner_item, regions);
                                spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
                                regionSpinner.setAdapter(spinnerArrayAdapter);
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        //displaying the error in toast if occurrs
                        Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
        // request queue
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }

    private void retrieveJSONCountry() {
        StringRequest stringRequest = new StringRequest(Request.Method.GET, URLCountry,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d("strrrrr", ">>" + response);
                        try {
                            JSONArray obj = new JSONArray(response);
                            if(1==1){
                                countryArrayList = new ArrayList<>();
                                for (int i = 0; i < obj.length(); i++) {
                                    CityClass playerModel = new CityClass();
                                    JSONObject dataobj = obj.getJSONObject(i);
                                    playerModel.setDescription(dataobj.getString("description"));
                                    countryArrayList.add(playerModel);
                                }

                                for (int i = 0; i < countryArrayList.size(); i++){
                                    countrys.add(countryArrayList.get(i).getDescription().toString());
                                }
                                ArrayAdapter<String> countryArrayAdapter = new ArrayAdapter<String>(MainActivity.this, simple_spinner_item, countrys);;
                                countryArrayAdapter.notifyDataSetChanged();
                                countryArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
                                countrySpinner.setAdapter(countryArrayAdapter);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        //displaying the error in toast if occurrs
                        Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
        // request queue
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnExplore:
                Intent intent = new Intent(this, GraphsActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }


}

GoodModel class

public class GoodModel {
    private String description;
    private int identifier;

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getIdentifier() {
        return String.valueOf(identifier);
    }

    public void setIdentifier(int identifier) {
        this.identifier = identifier;
    }
}

i'am using a volley for work with api

0

There are 0 best solutions below