1

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

0

2 Answers 2

2

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
            }

        }
    });
Sign up to request clarification or add additional context in comments.

1 Comment

The problem is I still have to return a String containing the dollar value for the item in my database because I need extend that information to the general product class.
1

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 ....

3 Comments

It won't let me return the String because "done" is void! But I can't change void to String because it would require refactoring core Parse files. Is there another way to do this?
Y ur returning inside 'done' ! .... Ur getting ur value to Eg: 'user' .... This complete query take into one method (function) and return at the end of the method ...
Check the accepted answer here: stackoverflow.com/questions/33705660/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.