如何填充多个JSONArrays

时间:2018-08-15 07:55:26

标签: android android-fragments android-recyclerview android-volley

我从JSON i得到响应,而我得到了两个以上的jSONArrays。像这样,我拍摄了logcat数据here is logcat data which I am fetching jsonarrays from my response

的快照

现在的问题是我如何在单个arraylist中填充所有这些arraylist,以及如何在recyclerView中显示。这是我的回复代码

JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(response);
                        JSONArray hotDealProduct =  jsonObject.getJSONArray("ListProduct");
                        setHotDealAdapter(hotDealProduct);
                        Log.e(TAG,hotDealProduct.toString());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

这是我的setHotDealAdapter()代码:

for (int i=0;i<hotDealProduct.length();i++) {
        try {
            JSONObject object = hotDealProduct.getJSONObject(i);
            final String discount = object.getString("discount");
            final String price = object.getString("Price");
            final String salePrice = object.getString("SalePrice");
            final String image = object.getString("MainImage");
            final String name = object.getString("Name");
            products.add(new HotDeal(1,discount, price ,image,salePrice,name));
            adapter = new HotDealAdapter(context,products);
            rvHotDeal.setLayoutManager(new GridLayoutManager(context,2));
            rvHotDeal.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        } catch (JSONException e) {
            e.printStackTrace();
        }

4 个答案:

答案 0 :(得分:0)

像这样更改您的setHotDealAdapter()

for (int i=0;i<hotDealProduct.length();i++) {
            try {
                JSONObject object = hotDealProduct.getJSONObject(i);
                final String discount = object.getString("discount");
                final String price = object.getString("Price");
                final String salePrice = object.getString("SalePrice");
                final String image = object.getString("MainImage");
                final String name = object.getString("Name");
                products.add(new HotDeal(1,discount, price ,image,salePrice,name));

            } catch (JSONException e) {
                e.printStackTrace();
            }
                adapter = new HotDealAdapter(context,products);
                rvHotDeal.setLayoutManager(new GridLayoutManager(context,2));
                rvHotDeal.setAdapter(adapter);
                adapter.notifyDataSetChanged();

答案 1 :(得分:0)

我看到您做得正确,但是您需要初始化适配器并将其设置在for循环之外,并且每次创建新适配器并将新适配器分配给回收站时都要使用它。

请尝试此代码

for (int i=0;i<hotDealProduct.length();i++) {
                try {
                    JSONObject object = hotDealProduct.getJSONObject(i);
                    final String discount = object.getString("discount");
                    final String price = object.getString("Price");
                    final String salePrice = object.getString("SalePrice");
                    final String image = object.getString("MainImage");
                    final String name = object.getString("Name");
                    products.add(new HotDeal(1,discount, price ,image,salePrice,name));

                } catch (JSONException e) {
                    e.printStackTrace();
                }
}

    adapter = new HotDealAdapter(context,products);
                    rvHotDeal.setLayoutManager(new GridLayoutManager(context,2));
                    rvHotDeal.setAdapter(adapter);

答案 2 :(得分:0)

这很容易,您只需要将适配器设置为循环外,

我的意思是以下代码:

adapter = new HotDealAdapter(context,products);
            rvHotDeal.setLayoutManager(new GridLayoutManager(context,2));
            rvHotDeal.setAdapter(adapter);

您必须立即调用它。由于完全更改了适配器,因此不需要以下行:

adapter.notifyDataSetChanged();

如果要一个接一个地添加项目,请首先设置适配器,然后获取适配器并向其中添加项目,然后调用adapter.notifyDataSetChanged();。 (此解决方案比较耗时,不建议使用)。

正确的代码:

try {
  for (int i=0;i<hotDealProduct.length();i++) {               
      JSONObject object = hotDealProduct.getJSONObject(i);
      String discount = object.getString("discount");
      String price = object.getString("Price");
      String salePrice = object.getString("SalePrice");
      String image = object.getString("MainImage");
      String name = object.getString("Name");
      products.add(new HotDeal(1,discount, price ,image,salePrice,name));
    }

    adapter = new HotDealAdapter(context,products);
    rvHotDeal.setLayoutManager(new GridLayoutManager(context,2));
    rvHotDeal.setAdapter(adapter);
} catch (JSONException e) {
  e.printStackTrace();
}

答案 3 :(得分:0)

对于最佳做法健壮性能->

A。。创建json响应的 POJO 模型

此步骤->

1)复制您的json响应并粘贴here

2)在其中选择 Source Type:JSON Annotation Style:Gson

3)根据您的选择,单击“生成”或“预览”,然后创建模型类。

B。。在build.gradle(Module:app)

中导入 GSON 依赖项。
implementation 'com.google.code.gson:gson:2.8.2'

C 。在您的活动或片段类中->

实施代码如下:

ArrayList<YourPOJO> itemlist = new ArrayList<>();

{
// Your code implementation of binding data to recyclerview : 

GsonBuilder gsonbuilder= new GsonBuilder();

Gson gson=gsonbuilder.create();
//response = its your json string from server
itemList =gson.fromJson(response, new TypeToken<List<YourPOJO>>(){}.getType());

yourAdapter = new YourAdapter(itemList);

yourRecyclerview.setAdapter(yourAdapter);

}

通过这种方法,您不必手动编写代码即可解析json并将其绑定到视图。

现在,如果您希望解析上面的代码中的json对象(在您的情况下,您只想解析JSONArray),请用response替换yourJsonArray.toString()

希望它可以解决您的问题。