简体   繁体   English

从Parse.com查询返回字符串

[英]Returning String from Parse.com query

So this doesn't seem to be working, but then again you can't return a String from a void method. 因此,这似乎不起作用,但是再次,您无法从void方法返回String。 The problem is I absolutely need to return a String based on how my classes are structured. 问题是我绝对需要根据类的结构返回一个String。 What can I do to accomplish this? 我该怎么做? I need to get a value for the price of the item. 我需要获取该商品的价格值。

@Override
public String getCost() {
final String[] productValue = {"null"};
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Inventory");
        query.whereEqualTo("productName", "Capris");
        query.findInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> list, ParseException e) {
                if (e == null) { //no exception, hence success
                    for (ParseObject productCost : list) {
                        productValue[0] = (String) productCost.get("productPrice");
                        // Cannot return a value from a method with void result type
                        return productValue[0];
                    }
                }
                else {
                    // Cannot return a value from a method with void result type
                    return null;
                }
            }
        });
        return null;
    }

Your conditions are wrong 你的条件错了

 @Override
    public String getCost() {
        final String[] productValue = {null};
        ParseQuery<ParseObject> query = new ParseQuery<ParseObject>
        query.whereEqualTo("productName", Capris);
        query.findInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> list, ParseException e) {
                if (e == null) { //no exception, hence success
                    productValue[0] = list.get(0).getString("productPrice");
                }
            }
        });
        return productValue[0];
    }

In above code productValue[0] may be null as its an aysnc call So replace findInBackground with find() 在上面的代码中productValue [0]可能为null,因为其aysnc调用因此将findInBackground替换为find()

public String getCost() {
    String productValue = null;
    ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Inventory");
    query.whereEqualTo("productName", "Capris");
    try {
        List<ParseObject> results = query.find();
        productValue = results.get(0).getString("productPrice");
        return productValue;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return productValue;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM