Adam First
Adam First

Reputation: 445

Android Parse - table of pointers to other tables, want to retrieve objects in the other tables

I feel like I'm pretty close on this one, just need the last bit.

I have the following tables:

_User (standard Parse table)

Category (object Id, name)

Exercises (object Id, name, description, thumbnail, image, etc)

and UserFavourites which is where I store the user's preferred exercises

(objectId, user->users table, exercise->exercises table, category->category table)

I have writing to Parse using pointers just fine:

               //create new parse object
                ParseObject favouriteExercise = new ParseObject("UserFavourites");
                //create pointers to the Exercise table and Category table
                ParseObject exercise = ParseObject.createWithoutData("Exercises", mExerciseId);
                ParseObject category = ParseObject.createWithoutData("Category", mCategoryId);
                //put those pointers into the Userfavourites table and save
                favouriteExercise.put("user",ParseUser.getCurrentUser());
                favouriteExercise.put("exercise",exercise);
                favouriteExercise.put("category",category);
                //save
                favouriteExercise.saveInBackground();

Now I'm trying to retrieve all the exercises a user has favourited and put them in to a listview by searching the table for any objects that match the user's pointer to the user's table:

    ParseQuery<Exercises> query = ParseQuery.getQuery("UserFavourites");
    final ParseObject user = ParseObject.createWithoutData(ParseUser.class, ParseUser.getCurrentUser().getObjectId());
    query.whereEqualTo("user", user);
    //call to parse.com to start the query

    query.findInBackground(new FindCallback<Exercises>() {
        @Override
        public void done(List<Exercises> exercises, ParseException e) {
            if (exercises != null) {
                Toast.makeText(getApplicationContext(), "Favourites found, can't list yet", Toast.LENGTH_SHORT).show();
                mAdapter.clear();
                //add all the exercises to the list
                mAdapter.addAll(exercises);
                //sort the list alphabetically
                mAdapter.sort(new Comparator<Exercises>() {
                    @Override
                    public int compare(Exercises exercises, Exercises t1) {
                        return exercises.getName().compareTo(t1.getName());
                    }
                });
            } else {
                mNoFavourites.setVisibility(View.VISIBLE);
            }

Where I'm stuck is when I run this I can see my query is working -> I am retrieving the 4 rows in UserFavourites that I favourited out of the table of 8, so it is filtering correctly, but the objects I'm getting aren't pointing to the exercises I want. They are just empty pointers.

Thanks.

enter image description here

Upvotes: 1

Views: 285

Answers (2)

Adam First
Adam First

Reputation: 445

I figured it out based on the logic kishore jethava gave.

I queried the favorites table, then with the results I wanted (which pointed to another table) I cycled through each result and got the object it pointed to and added it to my ArrayList.

public void getFavourites() {
    //set progress bar
    setProgressBarIndeterminateVisibility(true);
    ParseQuery<Exercises> query = ParseQuery.getQuery("UserFavourites");

    final ParseObject user = ParseObject.createWithoutData(ParseUser.class, ParseUser.getCurrentUser().getObjectId());
    query.whereEqualTo("user", user);

    query.include("exercise");
    //call to parse.com to start the query
    query.findInBackground(new FindCallback<Exercises>() {
        @Override
        public void done(List<Exercises> objects, ParseException e) {

            if (objects.size() != 0) {
                for(ParseObject object : objects)
                {
                    //for each pointer found, retrieve the object it points to
                    obj = object.getParseObject("exercise");
                    mAdapter.add((Exercises) obj);
                    }
                    });
                }
            } else {
                mNoFavourites.setVisibility(View.VISIBLE);
            }
            //stop progress bar
            setProgressBarIndeterminateVisibility(false);
        }
    });
}

Upvotes: 0

Kishore Jethava
Kishore Jethava

Reputation: 6834

Yes it will return only reference (Pointer). If you want actual object data call fetchInBackground

myObject.fetchInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
 if (e == null) {
  // Success!
 } else {
  // Failure!
 }
}
});

Upvotes: 1

Related Questions