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.

Paul Alexander
  • 2,686
  • 4
  • 33
  • 69

1 Answers1

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)

Palak
  • 1,276
  • 4
  • 17
  • 24
Bart de Ruijter
  • 917
  • 10
  • 21
  • 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?? – Paul Alexander Mar 16 '15 at 14:01
  • 1
    Something like this? String text = text.getString("text"); (I'll update answer aswell) – Bart de Ruijter Mar 16 '15 at 14:24
  • 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 – Paul Alexander Mar 16 '15 at 14:27