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(Config.JSON_ARRAY);
//Calling method getStudents to get the students from the JSON Array
getStudents(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 getStudents(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
students.add(json.getString(Config.TAG_LOCATION));
} catch (JSONException e) {
e.printStackTrace();
}
}
//Setting adapter to show the items in the spinner
spinner.setAdapter(new ArrayAdapter<String>(HomeActivity.this, android.R.layout.simple_spinner_dropdown_item, students));
}
/*SELECT BRANCH*/
private void getData3(){
//Creating a string request
StringRequest stringRequest = new StringRequest(Config_spinner3.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
result3 = j.getJSONArray(Config_spinner3.JSON_ARRAY);
//Calling method getStudents to get the students from the JSON Array
getStudents3(result3);
} 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 getStudents3(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
students3.add(json.getString(Config_spinner3.TAG_BRANCH));
} catch (JSONException e) {
e.printStackTrace();
}
}
//Setting adapter to show the items in the spinner
spinner3.setAdapter(new ArrayAdapter<String>(HomeActivity.this, android.R.layout.simple_spinner_dropdown_item, students3));
}
/*SELECT BRANCH ENDS HERE*/
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
/*
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
*/
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
尝试下面的代码。也许对你有帮助。
您的 Activity 使用 AdapterView.OnItemSelectedListener 实现
Spinner spinnerBloodGroup = view.findViewById(R.id.spinnerBloodGroup);
spinnerBloodGroup.setOnItemSelectedListener(this);
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Spinner spinner = (Spinner) parent;
if (spinner.getId() == R.id.spinnerGender) {
genderId = ListData.listGender.get(position).getId();
} else if (spinner.getId() == R.id.spinnerBloodGroup) {
bloodGroupId = ListData.listBloodGroup.get(position).getId();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
试试这个代码,也许会对你有帮助
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if(parent.getId() == R.id.spinner1)
{
String text1 = spinner1.getSelectedItem().toString();
}
else if(parent.getId() == R.id.spinner2)
{
String text2 = spinner2.getSelectedItem().toString();
}
}
或者尝试其他方式
ArrayList<LocationObjects> country=new ArrayList<>();
ArrayList<LocationObjects> country1=new ArrayList<>();
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if(parent.getId() == R.id.spinner1)
{
String text1 = country.get(position).getLocationName();
}
else if(parent.getId() == R.id.spinner2)
{
String text2 = country1.get(position).getLocationName();
}
}