简体   繁体   中英

display text with different return value on spinner Android

i have this json :

"result": [
    {
      "nama_p": "ACEH",
      "IDProvinsi": "1"
    },
    {
      "nama_p": "SUMATERA UTARA",
      "IDProvinsi": "6728"
    },
    {
      "nama_p": "SUMATERA BARAT",
      "IDProvinsi": "12920"
    }]

i have been trying to get IDProvinsi value when i display nama_p on my spinner.. bu im fail..

this is my java code :

 private void getData(){
        //Creating a string request
        StringRequest stringRequest = new StringRequest(Config.DATA_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        JSONObject j = null;
                        try {
                            //Parsing the fetched Json String to JSON Object
                            j = new JSONObject(response);

                            //Storing the Array of JSON String to our JSON Array
                            result = j.getJSONArray("result");

                            //Calling method getStudents to get the students from the JSON Array
                            getProv(result);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });

        //Creating a request queue
        RequestQueue requestQueue = Volley.newRequestQueue(this);

        //Adding request to the queue
        requestQueue.add(stringRequest);
    }

    private void getProv(JSONArray j){
        //Traversing through all the items in the json array
        for(int i=0;i<j.length();i++){
            try {
                //Getting json object
                JSONObject json = j.getJSONObject(i);

                //Adding the name of the student to array list
                id.add(json.getString("IDProvinsi"));
                provinsi.add(json.getString("nama_p"));
                System.out.println(provinsi);

//                MyClass[] obj ={
//                        new MyClass(provinsi,id)
//                };

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        //Setting adapter to show the items in the spinner

        spinnerprov.setAdapter(new ArrayAdapter<String>(AddLokasiActivity.this, android.R.layout.simple_spinner_dropdown_item,id));

    }

how do i get the value on IDProvinsi and send it to my other spinner i have 2 spinner
1. for display state 2. for display city

i want to display city when value of state send to my webservice

thx stackoverflow

i have change my spinner to search spinner .. its more efficient you can see here

[ Creating a text filter (like quick search) for a Spinner in Android

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM