Question

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);
Was it helpful?

Solution 2

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

OTHER TIPS

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
});

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...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top