如何处理选定项目上的多个微调器?我不知道该怎么办

时间:2018-12-13 06:19:53

标签: android spinner android-spinner

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) {

}

2 个答案:

答案 0 :(得分:0)

尝试以下代码可能会对您有所帮助

您的活动通过AdapterView.OnItemSelectedListener

实现
{ 
  "version": "1.0",
  "response": {
    "outputSpeech": {
      "type": "PlainText",
      "text": "Your Audio will start playing now"
    },
    "card": {
      "type": "Standard",
      "title": "Audio One Sample Title",
      "text": "Audio One Sample Description",
      "image": {
        "smallImageUrl": "https://image_url.jpeg",
        "largeImageUrl": "https://image_url.jpeg"
      }
    },
    "directives": [
      {
        "type": "AudioPlayer.Play",
        "playBehavior": "REPLACE_ALL",
        "audioItem": {
          "stream": {
            "url": "https://audio_url.mp3",
            "token": "sample_token_1",
            "offsetInMilliseconds": 0
          }
        }
      }
    ],
    "shouldEndSession": true
  }
}

答案 1 :(得分:0)

尝试此代码,可能会对您有所帮助

    @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(); 
            }
        }