Martin Erlic
Martin Erlic

Reputation: 5667

Retrieving String value from Parse Table

This should be fairly simple. I have a class called "Inventory". I have two important columns. "productName" and "productPrice". I need to get the productPrice based on the productName. How would I query for the name and then retrieve the associated price? The String is always returned as null.

Update:

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

This is more or less what I now have. Is there a better way to return a string? It's still coming back as null. I just need to return the value from the query.

Working Answer:

Returning String from Parse.com query

This was resolved in another question. In above code productValue[0] may be null as its an aysnc call So replace findInBackground with find()

Upvotes: 1

Views: 112

Answers (2)

Daft Dev
Daft Dev

Reputation: 58

productCost is a class of Parse Object Type. You are converting it to string when you should be rather accessing the member variable of class which contains the productValue. You should use something like this:

query.findInBackground(new FindCallback() {

        @Override
        public void done(List<ParseObject> objects,
                ParseException e) {
            if (e == null) {

                for (ParseObject productCost : objects) {
                   // use productCost.get(' product value columnName') to access the properties of the productCost object.
                }
            } else {
                //Query failed. There is an error
            }

        }
    });

Upvotes: 2

Anvesh523
Anvesh523

Reputation: 368

Try something like this .....

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) {
            for (ParseObject myData : list) {
            String user = (String) myData.get("productPrice");
            }
        }
        else {
          Toast.makeText(getApplicationContext(),
                         "Error: " + e.getMessage(),
                          Toast.LENGTH_SHORT).show();

              }
         }
    }

U can use String / int ... what ever data ur retrieving ....

Upvotes: 1

Related Questions