【发布时间】: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