【发布时间】:2019-06-22 09:05:36
【问题描述】:
我有一个数据库,其中有一列名为“items”,我将 JsonString 放在该列中。我想在 for 循环中从数组中获取数据,但它没有给我数据 这是我的代码:
dbConnector = new DbConnector(getContext(),null,null,1);
Cursor c = dbConnector.get().rawQuery("SELECT * FROM factors",null);
while (c.moveToNext()){
String name = c.getString(c.getColumnIndex("name"));
String items = c.getString(c.getColumnIndex("items"));
String itemsEncoded = null;
int priceEncoded = 0;
int amount = 0;
try {
JSONArray mJsonArray = new JSONArray(items);
JSONObject mJsonObject;
Log.e("JSON ARRAY",mJsonArray.length() + "");
for (int i = 0; i < mJsonArray.length(); i++) {
mJsonObject = mJsonArray.getJSONObject(i);
mJsonObject.getString("product_id");
priceEncoded += Integer.parseInt(mJsonObject.getString("price"));
amount += Integer.parseInt(mJsonObject.getString("number"));
itemsEncoded += mJsonObject.getString("product_name") + " - " + mJsonObject.getString("price") + " ????? " + " - " + mJsonObject.getString("number")+ " ???" + "\n";
}
}catch (JSONException e){
Log.e("ERROR",e.getMessage());
}
list.add(new FactorObject(name,priceEncoded + "",amount,itemsEncoded));
}
recyclerView = view.findViewById(R.id.factorRecycler);
adapter = new FactorsAdapter(getContext(),list);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
变量“priceEncoded”、“amount”、“itemsEncoded”都是空的,似乎for循环不起作用
JSON 示例: JSON Example
而且我 100% 确定我的数据库已满
【问题讨论】:
标签: android arrays json android-recyclerview