你如何找到当前与当前ParseUser关联的ParseRoles?

时间:2014-02-04 03:34:45

标签: java android android-listview parse-platform querying

我想使用当前ListView与之关联的不同Array的{​​{1}}或ArrayList来填充ParseRole。我认为这需要对当前用户进行某种查询,但是我只是不确定如何返回角色以及将它们返回到我可以填充的ParseUserArray中一个ArrayList。我知道你可以使用这个获得当前用户:

ListView

但是我无法从那里找到你如何建立用户被分配的角色。

编辑:

我现在也尝试过这个:

ParseUser.getCurrentUser();

但是,我似乎仍无法得到与此相关的任何工作。我目前已经尝试了所有我能想到的东西,并且因为“对象”的大小始终为0而无法实现。

提前致谢!

解答:

在实施了Marius Falkenberg Waldal的回答并将其移植到Android后面的逻辑后,我终于找到了适用于我的情况的解决方案。我决定发布它以防万一它在未来帮助任何人:

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

3 个答案:

答案 0 :(得分:2)

我在尝试确认某些内容时偶然发现了这一点,并意识到这有一个更简单的方法:

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

为了别人的缘故,这里有其他几种语言的答案。

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

答案 1 :(得分:1)

您可能需要遍历角色,检查当前用户是否在每个角色中。对不起,我不能为你提供一些android代码,因为我是一名iOS程序员,但这是你如何用iOS做的一个例子,希望你能把它移植到android。在iOS中,我会为此代码创建一个方法或类别:

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

希望这会让你走上正确的道路......

答案 2 :(得分:0)

像这样使用

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