[英]how to get the selected item id of the spinner using JSON parsing
I am a beginner to android API call.我是 android API 调用的初学者。 Please help me.
请帮我。 This is the method where I am trying to get the corresponding spinner id of the selected item.
这是我尝试获取所选项目的相应微调器 ID 的方法。 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.在 get Doctors() 方法中,我选择了名称,在注册 Online 方法中,我需要将所选微调项的 doc_id 传递给
API
调用。
How can i get the id by doing this.我怎样才能通过这样做来获得 id。
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试试这个我已经修改了你的 setOnItemSelectedListener 方法
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) {
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.