3

Hello
I'm playing around with Parse, I've managed to create a cloud database with two classes/tables, Users and Posts, I'm able to register and login users and allow them to make posts to the database.
I'm now trying to work out how to query the database and get the data that has been posted, my Posts class/table is made up of four columns: | ObjectID | text | user | createdAt |.
I would like to be able to select and display in my app, all of the data held in the 'text' column. I have very little database experience so sorry if this is a stupid question.

Code so far:

    ParseQuery<ParseObject> query = ParseQuery.getQuery("Posts");
    query.findInBackground(new FindCallback<ParseObject>() {

        @Override
        public void done(List<ParseObject> text, ParseException e) {

            if (e == null) {


                 Toast.makeText(MainActivity.this, text.toString(), Toast.LENGTH_LONG).show();

            }

            else {

                Toast.makeText(MainActivity.this, "query error: " + e, Toast.LENGTH_LONG).show();
            }

        }
    });

I know that I need to constrain the query to only data from the 'text' column so I'm obviously missing a line of code between line 1 and 2.

Any help would be appreciated. Thanks.

1 Answer 1

5

From the Parse API

You can restrict the fields returned by calling selectKeys with a collection of keys. To retrieve documents that contain only the score and playerName fields (and also special built-in fields such as objectId, createdAt, and updatedAt):

   ParseQuery<ParseObject> query = ParseQuery.getQuery("GameScore");
   query.selectKeys(Arrays.asList("playerName", "score"));
   List<ParseObject> results = query.find();

So to just retrieve the Text field, all you have to do is:

ParseQuery<ParseObject> query = ParseQuery.getQuery("Posts");
query.selectKeys(Arrays.asList("text"));
    query.findInBackground(new FindCallback<ParseObject>() {

        @Override
        public void done(List<ParseObject> posts, ParseException e) {

            if (e == null) {
                List<String> postTexts = new ArrayList<String>();
                for(ParseObject post : posts){
                   postTexts.add(post.getString("text"));
                }
                Toast.makeText(MainActivity.this, postTexts.toString(), Toast.LENGTH_LONG).show();
            }

            else {
                Toast.makeText(MainActivity.this, "query error: " + e, Toast.LENGTH_LONG).show();

            }

        }
    });

API reference

Doc reference (Just before the heading 'Queries on Array values)

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, thanks really helpful! One more question, when I execute this code, it seems to be returning the data's ID's/Keys, how do I get the actual string of text??
Something like this? String text = text.getString("text"); (I'll update answer aswell)
no worries, got it: String s = text.get(0).getString("text"); this returns the string thats in position 0 of the list of data. thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.