Query a Parse User - android

I am creating an app in android that allows the user to search other users registered in the app.
Each user is asked to enter a set of details like "Date of Birth" before entering into the app.These data are directly stored in the user class.
My requirement is that if a user enters a username and searches he must be able to see all the details available on that particular user.
For eg.:Lets assume every user must have a email(String) and date of birth(String).An User searches for name "John". What should the query be so that I can retrieve both email and DOB of a user with username "John".
The data given in the Parse.com guide is
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("username", name);
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> objects, ParseException e) {
if (e == null) {
Toast.makeText(getApplicationContext(),"Query Success",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),"Query Not Successful",Toast.LENGTH_LONG).show();
}
}
});
What are the changes that I need to make so that I can achieve my goal?
How to access the string stored inside the object?
If I retrieve multiple records from my query how can I handle it?
I have edited my query like this
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereMatches("username", name);
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> objects, ParseException e) {
if (e == null) {
for(ParseUser singleobject:objects){
String mail=singleobject.get("email").toString();
Toast.makeText(getApplicationContext(),mail,Toast.LENGTH_LONG).show();}
} else {
Toast.makeText(getApplicationContext(),"Query Not Successful",Toast.LENGTH_LONG).show();
}
}
});
No toast is generated but the query is executed without any exception.
Is the looping statement correct or am i doing something wrong??

What are the changes that I need to make so that I can achieve my goal?
The code looks like it correctly queries ParseUser for an exact match of a username. To match more loosely, consider using whereMatches
How to access the string stored inside the object?
Parse.Object provides getString() as well as other "get" variations depending on the attribute type.
If I retrieve multiple records from my query how can I handle it?
The "objects" parameter of the findInBackground completion handler is an array that contains zero or more matches to the query. Handle this array the way you would any other.

Related

Retrieved data from parse.com but it is in HashCode

I want to retrieve data from parse.com and show it in listview. but when i retrieve it and it shows me in hashcode. I have pasted picture of output.Please look into my query code and correct me.
Here is code for pulling data from parse.com
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> list, ParseException e) {
if (e != null) {
Toast.makeText(getActivity(), "Error" + e, Toast.LENGTH_SHORT).show();
} else {
for (ParseUser user : list) {
ParseUser newUser = new ParseUser();
newUser.getString(user.getString("username"));
newUser.getString(user.getString("Latitude"));
newUser.getString(user.getString("Longitude"));
newUser.getString(user.getString("Bloodtype"));
newUser.getString(user.getString("Mobile"));
user1.add(newUser);
}
ArrayAdapter<ParseUser> adapter = new ArrayAdapter<>(getActivity(),android.R.layout.simple_list_item_1,user1);
setListAdapter(adapter);
}
}
});
ArrayAdapter just use the toString of each object in the array.
Every user that you add to list is displayed as a pointer, this is the to string method of the ParseUser object.
If you are trying to display all information about the user you should create a custom adapter and implement the getView method.
If you want to check for now if it works correctly you can pass a list of just username for example and check if it works, then you can move on and implement your own adapter.
Edit:
For creating custom adapter check this question

ParseQuery for profile picture while displaying post

I am working on a sample app like Instagram in parse.com.Each post displays the profile picture(resized)of the uploader and the post photo.The user info is stored in a different class "_user" containing the profile picture in "profileimage" and the posts are stored in a different class "Posts".How to make a query to fetch them both (in a single query to reduce payload).The thing I have tried so far.I want to know what to add in my query.
ParseQuery<ParseObject> query=ParseQuery.getQuery("testing");
query.orderByAscending("createdAt");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> arg0, ParseException arg1) {
for(ParseObject obj:arg0){
ParseFile pfile=obj.getParseFile("Image");
coaching coach=new coaching();
coach.setId(obj.getObjectId());
coach.setName(obj.getString("Name"));
coach.setPlace(obj.getString("Place"));
coach.setImageurl(pfile.getUrl());
array.add(coach);
}
listView.setAdapter(new parseadapter());
}
});
What you're looking for is a relation, specifically a ParseRelation. You'll have to add a Pointer from your "Posts" class to your "Users" class. Do note that you add the user as a Pointer using their objectId. Something like:
Next you have to add the following code your query to include the Pointer object because by default the SDK does not.
query.include("postAuthor"); //your column name for pointer
Lastly you have to fetch the ParseObject containing the User data separately (in your loop)
ParseObject author = obj.getParseObject("postAuthor"); // column name for pointer
ParseFile authorPicture = author.getParseFile("profileimage"); // column user profile picture
authorPicture.getUrl(); // profile picture URL
author.getString("name"); // name of the user
Consider fetching the name of the user who posted* as I have above, this is a better database design and will correspond to changes made to the name of a user.
I hope this helps you and others stuck in similar situations. You can read more about ParseRelation here.

Updating a row in parse table using the object Id of the current device

Hi I am trying to update the location of the current device into the parse database using the objectId of this device.
ParseQuery innerQuery = new ParseQuery("_Installation");
innerQuery.whereEqualTo("objectId", ParseInstallation.getCurrentInstallation());//current phone
ParseQuery<PhoneFinder> query = ParseQuery.getQuery(PhoneFinder.class);
query.whereMatchesQuery("identification", innerQuery);
query.findInBackground(new FindCallback<PhoneFinder>() {
#Override
public void done(List<PhoneFinder> list, ParseException e) {
for (PhoneFinder loc : list)
{
loc.setLocation(geoPoint);
loc.saveInBackground();
}
}
});
If I use the objectId explicitly is works fine.
Any suggestions?
You haven't said what's wrong with the code you posted (that is, exactly what it does wrong) but I'm going to guess innerQuery doesn't return any rows. If that is the case, this information from the API docs may help:
Note: We only allow the following types of queries for installations:
query.get(objectId)
query.whereEqualTo("installationId", value)
query.whereMatchesKeyInQuery("installationId", keyInQuery, query)
whereEqualTo("objectId",value) isn't on that list, so that is probably why it's not working. Also, ParseInstallation.getQuery is the best way to get a query to use for installations.

Getting a ParseUser from a List<ParseUser> using Parse SDK for android?

Using the Parse SDK for android, I'm trying to receive a ParseUser from a query, the following code is what I have tried:
ParseQuery<ParseUser> query = ParseUser.getQuery();
Follow.include("author");
query.whereEqualTo("User", "Alex");
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> objects, ParseException e) {
if (e == null) {
// This is where I want to receive the ParseUser from the List<ParseUser>
ParseUser pu = objects.get("author");
} else {
}
} });
Object.get("author") creates errors.
Thankyou for reading :)
ParseUser pu = objects.get("author"); would be incorrect usage. Get method is generally used to get a object at a particular position in the list.
The query returns a list of ParseUsers which you have named as objects.
Now use an iterator to go through the list to access each ParseUser.
like this
for(ParseUser pu : objects){
//access the data associated with the ParseUser using the get method
//pu.getString("key") or pu.get("key")
}
Hope this helps!

How do I create groups of ParseUsers using Parse.com?

Currently I'm using Parse.com in order to create multiple ParseUsers. This works perfectly and each user can login individually. However from here I want to expand my app to allow Users to create groups of users and therefore have data that is only relevant and shared between these Users. This will mean that when the User logs in, they can see a List of the groups they are members of and from there can share data simply just to those users of that individual group. What would be the best way to tackle this and does anybody have any examples or tutorials that I could follow in order to understand this concept?
I've considered creating a Group class and then making this store User's IDs in an array and then allow each User to store an array of the Group IDs that they're currently members of. I'm just not really sure how to broach this issue.
Thanks in advance!
I ended up doing as shown below:
ParseQuery<ParseRole> query = ParseRole.getQuery();
Intent intent = getActivity().getIntent();
String groupId = intent.getStringExtra("groupId");
query.whereEqualTo("objectId", groupId);
groupUsers = new ArrayList<String>();
query.findInBackground(new FindCallback<ParseRole>() {
#Override
public void done(List<ParseRole> objects, ParseException e) {
if(e == null) {
for(ParseRole role : objects) {
ParseRelation<ParseUser> usersRelation = role.getRelation("users");
ParseQuery<ParseUser> usersQuery = usersRelation.getQuery();
usersQuery.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> objects, ParseException e) {
for(ParseUser user : objects) {
groupUsers.add(user.getUsername());
}
}
});
}
} else {
Toast.makeText(getActivity(), "ERROR", Toast.LENGTH_SHORT).show();
}
}
});
I passed in the group ID from the Intent that sent me to that Fragment that I was checking and then populated my ListView with the list that I've returned from the query on the Parse database with the specific group ID. I hope this helps anyone else who had the same issue as me. Good luck!
Since you probably want to use it for security as well as making it easier for code/users, look at the Roles security feature.
You can add/remove Users from Roles, and assign ACL permissions to Roles instead of Users. This way when people are added-to/removed-from the Role the permissions don't require any changes.
Initially there was a limit to the number of Roles you were allowed to create based on account type, but this restriction was removed last year I believe.

Categories

Resources