首先在微调器中显示选定的值

Display selected value in spinner first

如何在spinner列表中首先显示选中的项目? 假设 Rainy 是从 MySQL 检索到的,现在它应该首先显示 Rainy item。我如何实现这一目标?

  Spinner Weather;

   private void showEmployee(String json){
        try {
            JSONObject jsonObject = new JSONObject(json);
            JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);
            JSONObject c = result.getJSONObject(0);
            String weather = c.getString(Config.TAG_WEATHER);
            RetrieveWeather(weather);
            // what should add here

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

     public void RetrieveWeather(String a)
        {
            String[] arr = new String[]{"Sunny","Cloudy","Rainy","Thunderstorm"};
            List<String> list = new ArrayList<String>();
            String weather = a;
            list.add(weather);
            for(String s:arr){
                if(!list.contains(s)){
                    list.add(s);
                }
            }
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, list);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            Weather.setAdapter(adapter);
        }

尝试以下操作:

Spinner Weather;
String weather;
int pos = 0;
String[] arr = new String[]{"Sunny","Cloudy","Rainy","Thunderstorm"};

   private void showEmployee(String json){
        try {
            JSONObject jsonObject = new JSONObject(json);
            JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);
            JSONObject c = result.getJSONObject(0);
            weather = c.getString(Config.TAG_WEATHER);
            RetrieveWeather(weather);
            // what should add here

            Weather.setSelection(pos);
        } catch (JSONException e) {
            e.printStackTrace();
        }
      }

     public void RetrieveWeather(String a)
        {
            List<String> list = new ArrayList<String>();
            String weather = a;
            list.add(weather);
            for(String s:arr){
                if(!list.contains(s)){
                    list.add(s);
                }
            }

            for(int i=0;i<list.size();i++)
            {
               if(weather.equals(list.get(i)))
               {
                  pos = i;
               }
            }
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, list);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            Weather.setAdapter(adapter);
        }