0

i have a database with a column named "items" , i put my JsonString in that column. i want to grab data from array in a for loop but it is not giving me data here is my code :

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

variables "priceEncoded", "amount","itemsEncoded" are all null and it seems that for loop is not working

JSON example: JSON Example

and also i am 100% sure that my database is full

1 Answer 1

1

Your JSON doesn't contain "product_id" and mJsonObject will be null. it's better to use a model class to decode JSON, create a class that has your JSON fields

class JsonModelVM{
private String product_name,buyer_name;
private int number;
private double price;
  public void setProduct_name(String product_name) {
    this.product_name = product_name;
   }

public String getBuyer_name() {
    return buyer_name;
   }

public void setBuyer_name(String buyer_name) {
    this.buyer_name = buyer_name;
   }

public int getNumber() {
    return number;
   }

public void setNumber(int number) {
    this.number = number;
   }

public double getPrice() {
    return price;
   }

public void setPrice(double price) {
    this.price = price;
   }
}

and in decode class use Gson libray

implementation 'com.google.code.gson:gson:2.8.2'
.
. 
.
JsonModelVM object = new Gson().fromJson(mJsonArray.getJSONObject(i).toString(), 
AppCourseVM.class);

and it will return a object that you can use it ;)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.