0

I am getting data from the database, and populating the ArrayList

The variable ArrayList categories, is populated within the scope of the findInBackground. I noticed that the program never goes out of the findInBackground scope, so it never gets to return anything for my activity

 public ArrayList<Categorias> onCompleteGetData(String tableName) {

    ParseQuery<ParseObject> query = ParseQuery.getQuery(tableName);
    query.findInBackground((objects, e) -> {
        for (ParseObject objectList : objects) {
            Categorias categ = new Categorias();
            categ.setName(String.valueOf(objectList.get("name")));
            categ.setCategoriaId(String.valueOf(objectList.get("objectId")));
            this.categories.add(categ);
        }
    });

    return this.categories;
}

['House', 'loan', 'salary']

Part of My activity

            if (value == -1){
                categorias = new ArrayList<>();
                CategoryController controller = new CategoryController(context);
                categorias = controller.onCompleteGetData(Utils.CATEGORIA_EXPENSE);
                Toast.makeText(context,  String.valueOf(controller.onCompleteGetData(Utils.CATEGORIA_DESPESA).size()), Toast.LENGTH_SHORT).show();
            }else if (value == +1){
                categorias = new ArrayList<>();
                CategoryController controller = new CategoryController(context);
                categorias = controller.onCompleteGetData(Utils.CATEGORIA_REVENUE);
                Toast.makeText(context, String.valueOf(controller.onCompleteGetData(Utils.CATEGORIA_RECEITA).size()), Toast.LENGTH_SHORT).show();
            }
Zoe
  • 27,060
  • 21
  • 118
  • 148

1 Answers1

0

Since you are using findInBackground method, "return this.categories;" is running before (and therefore returning an empty list) than your loop. The easier way to fix your code would be using the find method instead of the findInBackground.

public ArrayList<Categorias> onCompleteGetData(String tableName) {

  ParseQuery<ParseObject> query = ParseQuery.getQuery(tableName);
  List<ParseObject> objectList = query.find();

  for (ParseObject objectList : objects) {
    Categorias categ = new Categorias();
    categ.setName(String.valueOf(objectList.get("name")));
    categ.setCategoriaId(String.valueOf(objectList.get("objectId")));
    this.categories.add(categ);
  }

  return this.categories;
}
Davi Macêdo
  • 2,954
  • 1
  • 8
  • 11