I want to add a new JsonObject inside a JsonArray if no items with the same name exist
I tried putting an else statement which creates the new object but every time I add an item it loops the whole array length and creates the object corresponding to the length of the array
for (int x = 0; x < array.length(); x++) {
try {
if(conn.getName().equals(array.getJSONObject(x).getString("name"))) {
array.getJSONObject(x).put("quantity", array.getJSONObject(x).getInt("quantity") +1);
itemList.add((String) array.getJSONObject(x).getString("name") + "\n" + "₱" + array.getJSONObject(x).getInt("quantity"));
Toast.makeText(MainActivity.this, "The item was successfully added! ", Toast.LENGTH_SHORT).show();
adapter.notifyDataSetChanged();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Aucun commentaire:
Enregistrer un commentaire