简体   繁体   中英

how to get the selected item id of the spinner using JSON parsing

I am a beginner to android API call. Please help me. This is the method where I am trying to get the corresponding spinner id of the selected item. In get Doctors() method I am selecting the name and in register Online method i need to pass the doc_id of the selected spinner item to API Call.
How can i get the id by doing this.

private void getData(String centerId) {
        //Creating a string request
    StringRequest stringRequest = new StringRequest(Request.Method.GET, Config.DATA_URL+centerId,
                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(Config.JSON_ARRAY);

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

                    }
                });


//Creating a request queue
        RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
//Adding request to the queue
        requestQueue.add(stringRequest);
    } 

    private void getDoctors(JSONArray j){ // Here in this method taking the spinner items 
        //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
                docList.add(json.getString(Config.TAG_FIRST_NAME) + " " + json.getString(Config.TAG_LAST_NAME));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        //Setting adapter to show the items in the spinner
        doctorLists.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, docList));



        doctorLists.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { //Here I am trying to get the id of the spinner, but I am not getting how to get the id.

                doc_id = docList.get(position);
             }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }





registerOnline(sharePreferenceManager.getUserLoginData(LoginModel.class).getResult().getCenterId(),
                                            sharePreferenceManager.getUserLoginData(LoginModel.class).getResult().getTabID()
                                        , ComponentUtils.getInputStringFromView(firstName)
                                        , ComponentUtils.getInputStringFromView(lastName)
                                        , gender
                                        , ComponentUtils.getInputStringFromView(mobileNumber)
                                        , ComponentUtils.getInputStringFromView(emailId)
                                        , ComponentUtils.getInputStringFromView(adharNumber)
                                        , ComponentUtils.getInputStringFromView(dateOfBirth)
                                        , doc_id
                                        , ComponentUtils.getInputStringFromView(referenceName)
                                        , ComponentUtils.getInputStringFromView(messageBox));

try this i have modified your setOnItemSelectedListener method

doctorLists.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { //Here I am trying to get the id of the spinner, but I am not getting how to get the id.

            doc_id = docList.getSelectedItem().toString();
         }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

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