【发布时间】:2018-09-18 16:18:18
【问题描述】:
我正在使用微调器来显示来自 json 的数据,但它在屏幕上什么也没显示。我是第一次使用,所以我想分享我为 spinner 所做的事情。我的代码是:
在 XML 中:
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner_location">
</Spinner>
活动中:
private Spinner spinner;
spinner=(Spinner)findViewById(R.id.spinner_location);
private class GetCity extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... strings) {
ContentValues param = new ContentValues();
JSONObject rootObj = parser.makeHttpUrlRequest(Constants.GET_CITY_LIST, "GET", param);
System.out.println("Result"+rootObj);
try{
if(rootObj != null) {
String status = rootObj.getString(Constants.SVC_STATUS);
if (Constants.STATUS_SUCCESS.equals(status)) {
JSONArray jsonArray = rootObj.getJSONArray("lstCity");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String city=jsonObject.getString("city");
cityLst.add(city);
}
}
}
} catch (JSONException ex) {
ex.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
spinner.setAdapter(new ArrayAdapter<String>
(DoctorListActivity.this, android.R.layout.simple_spinner_dropdown_item, cityLst));
}
我在 onCreate() 中调用这个类。 请尽快告诉我最佳答案。谢谢。
【问题讨论】:
-
在执行后完成所有操作
-
我也在那儿试过先生。我更新了我的问题
-
cityLst 大小是多少?
-
私有 ArrayList
cityLst = new ArrayList();这是我的城市列表,有 2 个城市进入此 -
在 for 循环检查 cityLst.size() 大于 0..如果它的 0 检查你的 json 数据
标签: android android-studio spinner android-spinner