0
我收到json數據並將其傳遞給Recyclerview。我的recyclerview sholud是2行和2行(共4項)。但是我的json數據不止這些。所以我通過下面的代碼限制了json數據(由AsyncTask解析)。問題在json數組長度和recyclerview
JSONArray data = json.getJSONArray("data");
for (int i = 0; i < 4; i++) {
JSONObject object = data.getJSONObject(i);
JSONObject product = object.getJSONObject("Products");
Album item = new Album();
item.setName(product.getString("pname"));
item.setPrice(Double.valueOf(formatData.format(product.getDouble("pprice"))));
display_deals_of_the_day.add(item);
}
在recyclerview中,列計數正常工作。但是行數有時會超過(並非所有時間)。任何人都可以幫助解決這個問題。我的回收查看代碼如下所述。
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(),2);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(1), true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
你需要證明你的適配器代碼 –
替換你的代碼是什麼,如果'JSONArray'有3個項目? – mihail