gpt4 book ai didi

java - 如何从 Presenter 解析 JSON 并设置为 RecyclerView(Android)

转载 作者:行者123 更新时间:2023-12-02 00:35:09 29 4
gpt4 key购买 nike

我尝试获取 xml 数据,然后将其解析为 JSON,我使用 OkHttp 作为连接。我设法从 LOG 获取数据,但当我登录到适配器且结果大小为 0 时,无法在 RecyclerView 中显示它

我设置了对模型和共享首选项的响应

问题的关键是我只是不明白如何从演示者那里获取响应,然后将其设置到主 fragment 中的适配器。

公共(public)类父类别{

@SerializedName("idkategori")
@Expose
private String idkategori;
@SerializedName("namakategori")
@Expose
private String namakategori;
@SerializedName("fileicon")
@Expose
private String fileicon;
@SerializedName("subkategori")
@Expose
private 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<>();

在这里,您正在将类别初始化为 new ArrayList<>();这就像您正在创建一个新的数组列表。只需删除此行即可。

关于java - 如何从 Presenter 解析 JSON 并设置为 RecyclerView(Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57984451/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com