gpt4 book ai didi

java - 解析 json 对象并附加到 recyclerview

转载 作者:太空宇宙 更新时间:2023-11-04 11:08:05 25 4
gpt4 key购买 nike

我刚刚开始学习java和Android,我正在尝试解析json数据并将数据应用到recyclerview,但我无法做到这一点。这是我的代码

public void JSON_DATA_WEB_CALL(){

jsonArrayRequest = new JsonArrayRequest(GET_JSON_DATA_HTTP_URL,

new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
progressBar.setVisibility(View.INVISIBLE);
JSON_PARSE_DATA_AFTER_WEBCALL(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
});

requestQueue = Volley.newRequestQueue(this);

requestQueue.add(jsonArrayRequest);
}

public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array){

for(int i = 0; i<array.length(); i++) {

GetDataAdapter GetDataAdapter2 = new GetDataAdapter();

JSONObject json = null;
try {

json = array.getJSONObject(i);

GetDataAdapter2.setImageTitleNamee(json.getString(JSON_IMAGE_TITLE_NAME));
//GetDataAdapter2.setImageServerLarger(json.getString(JSON_IMAGE_LARGER));

GetDataAdapter2.setImageServerUrl(json.getString(JSON_IMAGE_URL));
GetDataAdapter2.setMrp_price(json.getString(JSON_MRP_PRICE));
GetDataAdapter2.setDisc_price(json.getString(JSON_DISC_PRICE));

} catch (JSONException e) {

e.printStackTrace();
}
GetDataAdapter1.add(GetDataAdapter2);
}

recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1, this);

recyclerView.setAdapter(recyclerViewadapter);
}

这是我的 JSON 响应

{"118":{"garment_color":"Blue","garment_name":"skjhkds","garment_price":"232"},"119":{"garment_color":"hjsadjjs","garment_name":"sdasd","garment_price":"23478"}}

请有人简要解释一下正确的代码。这将会非常有帮助。谢谢

最佳答案

rvAdapter = new RvAdapter(getActivity());
recyclerview.setAdapter(rvAdapterHScode);
rvAdapter.set(responce.getcodes());
recyclerview.setLayoutManager(new GridLayoutManager(getActivity(), 3));

然后在适配器中接受设定值或

在 recyclerview 适配器中调用您的响应

关于java - 解析 json 对象并附加到 recyclerview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46278405/

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