1

What I've essentially built is an EditTextView that collects a String, which when input by the user attempts to communicate with the back-end in order to pull the user's "objectId", this being a column in my User database.

The showProfileActivity() function shown below simply works, and I've excluded it because it isn't relevant to the problem. It takes the user's "objectId" and shows the user's profile.

What I want is for "String userId", which is the User's "objectId" in the database to be generated dynamically. I know that the showProfileActivity() function works because I can literally just input a String with the hard-coded objectId and it brings me to that user's profile.

My question is, based on the String input by the user into the search field, how can I retrieve the value in the relevant "objectId" column?

findViewById(R.id.submitSearch).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                EditText searchUserTextField = (EditText) findViewById(R.id.searchUserTextField);

                // Value input to search field
                final String searchInput = searchUserTextField.getText().toString();
                // System.out.println(searchInput);

                // Initiate ParseQuery
                final ParseQuery<ParseUser> query = ParseQuery.getQuery("username");

                // Look for the username that was typed into the search field
                query.whereEqualTo("username", searchInput);

                query.findInBackground(new FindCallback<ParseUser>() {
                    public void done(List<ParseUser> objects, ParseException e) {
                        if (e == null) {
                            // The query was successful.

                            // This works but clearly always loads the exact same user profile. I need this string to be loaded dynamically as a function of the user's search query.
                            String userId = "dj16qsXPle";
                            // System.out.println(userId);

                            showProfileActivity(userId);

                        } else {
                            // Something went wrong.
                        }
                    }
                });

                // Execute RemoteDataTask AsyncTask
                new RemoteDataTask().execute();
            }
        });
    }

2 Answers2

1

I am not sure what showProfileActivity does, but since you are passing it a userId I am assuming it does another query? Why not do the following:

Assuming usernames are unique (in this case it seems you are also assuming that) Then the findInBackground method should return just a single user.

query.findInBackground(new FindCallback<ParseUser>() {
                public void done(List<ParseUser> objects, ParseException e) {
                    if (e == null) {
                        // The query was successful.
                        if(objects.size() > 0){
                            ParseUser user = objects.get(0);
                            showProfileActivity(user);
                        }
                    } else {
                        // Something went wrong.
                    }
                }
            });

And in showProfileActivity, use the user data do whatever showProfileActivity does.

Or, if you are using the objectId for something else, you can do this:

ParseUser user = objects.get(0);
showProfileActivity(user.getObjectId());

and this gives you the id you are looking for instead of the hardcoded one. Again, doing in showProfileActivity whatever it is you do with the objectId. This could mean retrieving from a separate table using objectId, etc..

Lucas Crawford
  • 3,078
  • 2
  • 14
  • 25
  • Hmm... Very close it seems, but I'm getting an IndexOutOfBoundsException. java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0. What could this possibly mean? – drearypanoramic Oct 14 '15 at 18:57
  • That means that no users matched. Let me edit for you real quick. Make sure that the username input DOES exist in the Parse database and try one that does exist. Also add the modified code after i edit – Lucas Crawford Oct 14 '15 at 18:59
  • Yeah, it's returning an empty list, which is really weird since these usernames simply must exist. I/System.out﹕ [] – drearypanoramic Oct 14 '15 at 19:00
  • edit only checks the size of the results. The fact that e == null means the query successfully was executed, but there was no users found. Use Log.e or Log.d when printing to console, it's best practice and easier to see in the logs (Log.e is bright red!) – Lucas Crawford Oct 14 '15 at 19:00
  • What does the Task do you are calling? The asynctask RemoteDataTask – Lucas Crawford Oct 14 '15 at 19:01
  • It queries another object used in the activity, but doesn't seem to be affecting anything as I've commented all of that out and I get the same result. Just an empty list. – drearypanoramic Oct 14 '15 at 19:13
  • Perhaps there's something wrong with this line: query.whereEqualTo("username", searchInput); ? It doesn't seem to be querying correctly based on the text input provided by the user. – drearypanoramic Oct 14 '15 at 19:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/92313/discussion-between-drearypanoramic-and-lucas-crawford). – drearypanoramic Oct 14 '15 at 20:50
1

Ultimately figured it out. I granted you the answer because you definitely provided the core of the correct solution. My error was in initiating my ParseQuery as such: final ParseQuery query = ParseQuery.getQuery("username");. It should have just been ParseQuery query = ParseUser.getQuery();

                // Initiate ParseQuery
                ParseQuery query = ParseUser.getQuery();
                query.whereEqualTo("username", searchInput);
                query.findInBackground(new FindCallback<ParseUser>() {

                    @Override
                    public void done(List<ParseUser> objects, ParseException e) {
                        if (e == null) {
                            // The query was successful.
                            System.out.println(objects);
                            ParseUser user = objects.get(0);
                            String userId = user.getObjectId();
                            showProfileActivity(userId);

                        } else {
                            // Something went wrong. Look at the ParseException to see what's up.
                        }
                    }
                });