我尝试获取xml数据,然后将其解析为JSON,我使用OkHttp作为连接。我设法从LOG获取数据,但是当我登录到适配器且结果为0时,我无法在RecyclerView中显示数据我将响应设置为模型和sharedpreference问题的重点在于,我只是不了解如何从演示者那里获得响应,然后将其设置为主Fragment中的适配器。公共类ParentCategories {@SerializedName("idkategori")@Exposeprivate String idkategori;@SerializedName("namakategori")@Exposeprivate String namakategori;@SerializedName("fileicon")@Exposeprivate String fileicon;@SerializedName("subkategori")@Exposeprivate SubCategories subkategori;public ParentCategories(Parcel in) { this.idkategori = in.readString(); this.namakategori = in.readString(); this.fileicon = in.readString();}public ParentCategories() {}public String getIdkategori() { return idkategori;}public void setIdkategori(String idkategori) { this.idkategori = idkategori;}public String getNamakategori() { return namakategori;}public void setNamakategori(String namakategori) { this.namakategori = namakategori;}public String getFileicon() { return fileicon;}public void setFileicon(String fileicon) { this.fileicon = fileicon;}public SubCategories getSubkategori() { return subkategori;}public void setSubkategori(SubCategories subkategori) { this.subkategori = subkategori;}}public class CategoriesPresenter {....public void onResponse(Call call, Response response) throws IOException { String mMessage = response.body().string(); JSONObject jsonObj = null; try { jsonObj = XML.toJSONObject(mMessage); JSONObject jsonObject = new JSONObject(jsonObj.toString()); JSONObject object = jsonObject.getJSONObject("posh"); String attr2 = object.getString("resultcode"); com.davestpay.apphdi.helper.Log.d("hasil", String.valueOf(object)); if (attr2.equalsIgnoreCase("0000")) { String idAgen = object.getString("idagen"); int jumlahKategori = object.getInt("jumlahkategori"); JSONArray category = object.getJSONArray("kategori"); List<ParentCategories> parentCategories = new ArrayList<ParentCategories>(); for (int i = 0; i < category.length(); i++) { ParentCategories categories = new ParentCategories(); JSONObject c = category.getJSONObject(i); Log.d(TAG, "onResponseC: "+c); String idKategori = c.getString("idkategori"); String namaKategori = c.getString("namakategori"); Log.d(TAG, "onResponseNamaKategori: "+namaKategori); String fileIcon = c.getString("fileicon"); JSONObject subCategories = c.getJSONObject("subkategori"); JSONArray subCategory = subCategories.getJSONArray("kategori2"); Log.d(TAG, "onResponseSubCategories: "+subCategory); for (int subCatPosition = 0; subCatPosition < subCategory.length(); subCatPosition++) { SecondCategories secondCategories = new SecondCategories(); List<SecondCategories> listSecondCategories = new ArrayList<>(); JSONObject sc = subCategory.getJSONObject(subCatPosition); String secIdKategori = sc.getString("idkategori"); String secNamaKategori = sc.getString("namakategori"); String secFileIcon = sc.getString("fileicon"); secondCategories.setIdkategori(secIdKategori); secondCategories.setNamakategori(secNamaKategori); secondCategories.setFileicon(secFileIcon); listSecondCategories.add(secondCategories); } categories.setIdkategori(idKategori); categories.setNamakategori(namaKategori); categories.setFileicon(fileIcon); parentCategories.add(categories); Log.d(TAG, "onResponseFinalCategories: "+parentCategories); } iCategories.onSuccessCategories(parentCategories); preferenceHelper.clear(PreferenceHelper.CATEGORIES); preferenceHelper.putList(PreferenceHelper.CATEGORIES, parentCategories); } else { Log.d(TAG, "onResponse: "); } } catch (JSONException e) { com.davestpay.apphdi.helper.Log.e("JSON exception", e.getMessage()); e.printStackTrace(); } }}private void getInit() { if (preferenceHelper != null) { idAgen = preferenceHelper.getString(PreferenceHelper.ID_AGEN); namaAgen = preferenceHelper.getString(PreferenceHelper.NAMA_AGEN); password = preferenceHelper.getString(PreferenceHelper.PASSWORD); categories = preferenceHelper.getList(PreferenceHelper.CATEGORIES, ParentCategories[].class); } authPresenter = new AuthPresenter(getContext()); presenter = new CategoriesPresenter(); presenter.setBaseView(this); presenter.onCreate(getContext()); if (authPresenter.isLoggedIn()) {// kategori.setText(categories.toString()); presenter.getCategories(idAgen, password, counter); } kategori = mView.findViewById(R.id.kategori); categories = new ArrayList<>(); rvMain = mView.findViewById(R.id.rv_categories); adapter = new CategoriesListViewAdapter(getContext(), categories); layoutManager = new LinearLayoutManager(getdActivity()); adapter.notifyDataSetChanged(); rvMain.setLayoutManager(layoutManager); rvMain.setAdapter(adapter); } 最佳答案 这就是问题。 categories = new ArrayList<>();在这里,您正在将类别初始化为新的ArrayList ();。就像您正在创建一个新的arraylist。只需删除此行。 07-24 09:50