I want to populate a ListView
with an Array
or an ArrayList
of the different ParseRole
s 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);