How to return object from an anonymous inner class in java

938 views Asked by At

If I have the following function :

        public class Product {

    public String barCode;
    public String name;
    public Category category;
    public double price;
    public Store store;
    WebService ws = new WebService();

    public product getProduct(String barcode) {

        final Product p = new Product();
        ParseQuery<ParseObject> query = ParseQuery.getQuery("Product");
        query.getInBackground("XxfqN425pK", new GetCallback<ParseObject>() {
            public void done(ParseObject object, ParseException e) {
                if (e == null) {
                    Log.d("((((( name )))))", name);

                    String name1 = "p=" + object.getString("product_name");
                    p.name = name1;

                } else {
                    p.name = "erro" + e.getMessage();

                }
            }
        });
        return p;

    }

    public Product get() {
        return this;
    }

}

why p.name coming from the getProduct() call is always null despite the Log statement display a value for name

where to call the function:

class getProduct extends AsyncTask {

@Override
protected Product doInBackground(Void... arg0) {
    Product p = new Product();
    return p.getProduct("");
}

@Override
protected void onPostExecute(Product result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);
    Log.d("Product name >>>", result.name + "--");
}

}

0

There are 0 answers