从辅助类向我的活动返回解析查询数据

时间:2019-05-12 16:20:10

标签: android parse-platform parse-server back4app

我正在从数据库中获取数据,并填充ArrayList

变量ArrayList类别填充在findInBackground的范围内。我注意到该程序永远不会超出findInBackground范围,因此它永远不会为我的活动返回任何内容

 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;
}

[“房子”,“贷款”,“工资”]

我的活动的一部分

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

1 个答案:

答案 0 :(得分:0)

由于您使用的是findInBackground方法,因此请“返回this.categories;”。在循环之前(因此返回空列表)运行。修复代码的更简单方法是使用find方法而不是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;
}