所以这似乎不起作用,但是再次你不能从void方法返回一个String。问题是我绝对需要根据我的类的结构来返回一个String。我该怎么做才能做到这一点?我需要得到物品价格的价值。
@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;
}
答案 0 :(得分:2)
你的条件错了
@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()
替换findInBackgroundpublic 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;
}