4

I want to populate a ListView with an Array or an ArrayList of the different ParseRoles that the current ParseUser has associated with them. I presume this requires some kind of query on the current User, however I'm just not really sure how you return the Roles as well as return them into an Array or an ArrayList with which I could populate a ListView. I know you can get the current user using this:

ParseUser.getCurrentUser();

However I can't from there seem to find how you establish the Roles that the User has been allocated.

EDIT:

I've now also tried experimenting with this:

ParseQuery<ParseRole> query = ParseRole.getQuery();
query.whereEqualTo("users", ParseUser.getCurrentUser().getObjectId());
query.findInBackground(new FindCallback<ParseRole>() {
   @Override
   public void done(List<ParseRole> objects, ParseException e) {
      if(e == null) {
         Toast.makeText(getApplicationContext(), objects.size() + "", Toast.LENGTH_SHORT).show();
      } else {
         Toast.makeText(getApplicationContext(), "ERROR", Toast.LENGTH_SHORT).show();
      }
   }                      
});

But I still can't seem to get anything to work related to that either. I've currently tried everything I can think of and aren't really getting anywhere as the size of 'objects' is still always 0.

Thanks in advance!

ANSWER:

After implementing the logic behind Marius Falkenberg Waldal's answer and porting it to Android, I managed to finally get a solution that worked for my situation. I've decided to post it in case it helps anyone in the future:

    ParseQuery<ParseRole> roleQuery = ParseRole.getQuery();
    List<ParseRole> allRoles = null;
    try {
        allRoles = roleQuery.find();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    userRoles = new ArrayList<ParseRole>();
    userRolesNames = new ArrayList<String>();
    for(ParseRole role : allRoles) {
        ParseQuery usersQuery = role.getRelation("users").getQuery();
        usersQuery.whereEqualTo("objectId", ParseUser.getCurrentUser().getObjectId());
        try {
            if(usersQuery.count() > 0) {
                userRoles.add(role);
                userRolesNames.add(role.getName().toString());
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }               

    GroupAdapter adapter = new GroupAdapter(this, userRolesNames);
    workspaces.setAdapter(adapter);
edwoollard
  • 12,245
  • 6
  • 43
  • 74
  • You were almost there with your check against users, but you compare it to the user object, not just the ID. See my answer below. – Timothy Walters Nov 26 '14 at 10:18

3 Answers3

2

I stumbled across this while trying to confirm something, and realised there's a much simpler way:

Android

ParseQuery<ParseRole> query = ParseRole.getQuery();
query.whereEqualTo('users', ParseUser.getCurrentUser());
query.findInBackground(new FindCallback<ParseRole>() {
    public void done(List<ParseRole> roles, ParseException e) {
        // do your thing with the roles list
    }
});

And for the sake of others, here's the answer in a couple of other languages.

iOS Objective-C

PFQuery *query = [PFRole query];
[query whereKey:@"users" equalTo:[PFUser currentUser]];
[query findObjectsInBackgroundWithBlock:^(NSArray *roles, NSError *error) {
  if (!error) {
    // Do something with the found roles
  }
}];

iOS Swift

var query = PFRole.query();
query.whereKey('users', equalTo:PFUser.currentUser());
query.findObjectsInBackgroundWithBlock {
  (roles: [PFObject]!, error:NSError!) -> Void in
  if error == nil {
    // do your thing with the roles
  }
}

JavaScript

var query = new Parse.Query(Parse.Role);
query.equalTo('users', Parse.User.current());
query.find().then(function(roles) {
  // do your thing with the roles array
});
Timothy Walters
  • 16,866
  • 2
  • 41
  • 49
1

You would probably need to iterate through the roles, checking if the current user is in each of them. Sorry I can't provide you with some android code, as I am an iOS programmer, but this is an example of how you could do it with iOS, and hopefully you can port this to android. In iOS I would make a method, or maybe a category, for this code:

PFQuery *rolesQuery = [PFRole query]; 
NSArray *allRoles = [rolesQuery findObjects];
NSMutableArray *userRoles = [NSMutableArray arrayWithCapacity:10];
for (PFRole *role in allRoles) {
    PFQuery *usersQuery = [role relationforKey:@"users"].query;
    [usersQuery whereKey:@"objectId" equalTo:[PFUser currentUser].objectId];
    if ([usersQuery countObjects]) {
        [userRoles addObject:role];
    }
}

Hope this puts you on the right path...

Marius Waldal
  • 9,537
  • 4
  • 30
  • 44
  • What exactly is the last if statement doing as I'm struggling to interpret that in Android? – edwoollard Feb 05 '14 at 10:49
  • PFQuery *usersQuery = [role relationforKey:@"users"].query; [usersQuery whereKey:@"objectId" equalTo:[PFUser currentUser].objectId]; These lines makes sure you only return roles containing the current user if ([usersQuery countObjects]) { This is the same as saying "If the query returns more than 0 results" – Marius Waldal Feb 05 '14 at 11:23
  • 1
    Argh, formatting! The simple answer to your question: This is the same as saying "If the query returns more than 0 results" – Marius Waldal Feb 05 '14 at 11:27
  • Finally managed to get it to work. Thanks so much for your help man. I'll post in the question what I ended up with but I feel you should still get the accepted answer as you gave me the logic. :) – edwoollard Feb 05 '14 at 14:04
  • Cheers, mate! Glad you got it to work. And thanks for posting the final code :-) – Marius Waldal Feb 06 '14 at 07:44
0

Use like this

ParseRole myroles= (ParseRole) new ParseQuery("Role").whereEqualTo("name", "AppName-"+currentuserid).getFirst();


Here  "name" -----------Column in Role Table

      "AppName" --------eg: "MyApp-23"   in your _User table



I think this helps...
KP_
  • 1,854
  • 25
  • 43
  • I'm not sure if this would exactly work but this would only give me the Users from one specified Role? I just want all of the Roles of the Current User. How can this be achieved? – edwoollard Feb 04 '14 at 20:08