从 Parse.com 查询返回字符串 [英] Returning String from Parse.com query

查看:47
本文介绍了从 Parse.com 查询返回字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这似乎不起作用,但是您又不能从 void 方法返回 String.问题是我绝对需要根据我的类的结构来返回一个字符串.我能做些什么来实现这一目标?我需要获取商品价格的值.

So this doesn't seem to be working, but then again you can't return a String from a void method. The problem is I absolutely need to return a String based on how my classes are structured. 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;
    }

推荐答案

你的条件有误

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

在上面的代码中 productValue[0] 可能是 null 作为它的 aysnc 调用所以用 find() 替换 findInBackground

In above code productValue[0] may be null as its an aysnc call So replace findInBackground with 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;
}

这篇关于从 Parse.com 查询返回字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆