2015-11-14 152 views
0

所以这似乎并没有工作,但然后再次无法从void方法返回一个字符串。问题是我绝对需要根据我的类的结构来返回一个字符串。我该怎么做才能做到这一点?我需要获得物品价格的价值。从Parse.com返回字符串查询

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

回答

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

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

权更换findInBackground。感谢您指出,但它仍然没有解决的问题,我不能返回一个无效方法的字符串。无论哪种方式,我需要提取该字符串。我的getCost()方法必须返回一个String。 – santafebound

+0

我在评论中发现我的错误。 – santafebound

+0

让我知道,如果它不起作用 – abhishesh