Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query a Parse User

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();
        }
    }
});
  1. What are the changes that I need to make so that I can achieve my goal?

  2. How to access the string stored inside the object?

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

like image 745
Nirmal Raj Avatar asked Sep 26 '22 00:09

Nirmal Raj


1 Answers

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

  1. How to access the string stored inside the object?

Parse.Object provides getString() as well as other "get" variations depending on the attribute type.

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

like image 140
danh Avatar answered Oct 13 '22 01:10

danh