简体   繁体   中英

Android Parse.com Pointers

I have two classes in Parse : (User and Games). My columns names are:

User(objectId,username,password,email,phone). Games(objectId,name,PlayedBy{this is a pointer to the User class}).

My Problem is that I can query all the games of a single user since it's one to many relation but I don't know how to get the user who played a specific game, in the parse guide for Android I've found this:

// say we have a Game object ParseObject game = ... // getting the user who created the Game ParseUser createdBy = game.getUser("createdBy");

but it seems there is an error with the game.getUser like it's writing wrong. I've tried:

ParseUser createdBy = game.getParseUser("PlayedBy");

and iI don't know what to do next. the query I've used to get all the games for a specific user is :

    public void queryGamesFromParse(){
    ParseQuery<ParseObject> gamesQuery = new ParseQuery<>("Games");
    gamesQuery.whereEqualTo("PlayedBy", ParseUser.getCurrentUser());
    gamesQuery.include("PlayedBy");
    gamesQuery.orderByDescending("createdAt");
    gamesQuery.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> List, ParseException e) {
            if(e == null){

                Adapter adapter = new Adapter(MainActivity.this, List);
                setListAdapter(adapter);

            }else{
               Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
            }
        }
    });
}

from the database in parse when I click the PlayedBy column it go directly to the User table. I want to create a query that looks for the players who played a specific game and show me there username in a list just like I did with the first one. If anyone can help me with that, that would be appreciated thank you.

Games.playedBy as type Array and array.entry = pointer to _User class

--EDITED-- use the data browser on 'playerBy' verify each entry in the array is a pointer....

{"__type":"Pointer","className":"_User","objectId":"OID.value"}

and not

{"objectId":"someUserID"}

When you query Games , then you fetch the 'playedBy' array and you would have all the _User.name values that you could provide to the View adapter managing the list.

 queryGames.include("playedBy")  // inlines each full _User in result

Or you could have a 'plays' class that is just 2 pointers and a date

plays.game = pointer to Game plays.user = pointer to _User

then you can use 'query.include.game' + 'query.include.user' to flatten the query ( no subsequent fetch reqd )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM