我試圖將我的 json 數據放入 listview 屬性,但數據未顯示在屬性中。數據顯示在運行窗口中。如何將數據放入屬性中?我使用 fragment 來顯示 listview enter image description here我的主要片段:api = retrofitClient.getClient("http://10.8.0.2:5000/").create(Api.class); String encoding = Base64Encoder.encode(UtilsApi.username + ":" + UtilsApi.password); Log.d(TAG, "listsensor: "+api.listSensor(UtilsApi.username,UtilsApi.password,"Basic "+encoding)); api.listSensor(UtilsApi.username, UtilsApi.password, "Basic " + encoding) .enqueue(new Callback<List<Sensor>>() { @Override public void onResponse(Call<List<Sensor>> call, Response<List<Sensor>> response) { if (response.isSuccessful()) { try { Toast.makeText(getActivity().getApplicationContext(),"Success",Toast.LENGTH_SHORT).show(); MediaType JSON = MediaType.parse("application/json; charset=utf-8"); OkHttpClient client = new OkHttpClient(); JSONObject jsonRESULTS = new JSONObject(response.body().toString()); sensors = (List<Sensor>) response.body(); listView.setAdapter(new SensorListAdapter(getActivity().getApplicationContext(), sensors)); }catch (JSONException e){ e.printStackTrace(); } }else { Toast.makeText(getActivity().getApplicationContext(), "Sensor Failed", Toast.LENGTH_LONG).show(); } } } }); }
1 回答

天涯盡頭無女友
TA貢獻1831條經驗 獲得超9個贊
根據 Json 結構,列表本身是外部 Json 對象的一部分。讓我們說 SensorsObject,所以使用 SensorsObject 作為響應,List 將是此類的屬性。
添加回答
舉報
0/150
提交
取消