【问题标题】:Display selected value in spinner first首先在微调器中显示所选值
【发布时间】:2016-01-02 23:44:13
【问题描述】:

如何在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);
        }

【问题讨论】:

    标签: android json spinner android-spinner


    【解决方案1】:

    尝试以下方法:

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

    【讨论】:

    • 如果我有 10 个项目,我需要写到 10 吗?
    • 不。你可以把它放在一个循环中来做到这一点。在目前的状态下它对你有用吗?
    • 它实际上取决于list 中的字符串序列,而不是arr 中的序列。我只是举了一个例子给你一个粗略的想法。查看list 中的字符串顺序。
    • 如何让它显示雷暴?我不知道
    • 干杯...快乐的编码:)
    猜你喜欢
    • 2013-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多