簡體   English   中英

如何填充多個JSONArrays

[英]How to populate multiple JSONArrays

我收到了來自JSON i的響應,而我得到了兩個以上的jSONArrays。 像這樣我拍了logcat數據的快照, 這里是我從響應中獲取jsonarrays的logcat數據

現在的問題是我如何在單個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();
        }

像這樣更改您的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();

我看到您的操作正確,但是您需要初始化適配器並將其設置在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);

很簡單,您只需要將適配器設置為非循環,

我的意思是以下代碼:

 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(); } 

對於最佳實踐強大的性能- >

A.創建POJO或JSON響應模型

對於此步驟->

1)復制您的json響應並粘貼到此處

2)選擇“ 源類型:JSON”和“ 注釋樣式: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),請在yourJsonArray.toString()替換response

希望它能解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM