AspiringDeveloper
AspiringDeveloper

Reputation: 125

How to retrieve objects from Parse.com database and show it in Android TextView?

I am building a simple MCQ quiz app.

What I did was make a database on Parse.com and stored the questions, possible answers and the correct solution in it.

The Parse.com table can be seen in this image: https://i.sstatic.net/oydiB.png

As you can see, each object has a question, with 4 possible options and the correct answer.

Now, I want to show the questions inside a TextView and the options inside four different buttons. The user will click on the correct button to answer and the app will check if the correct button was pressed.

How do I retrieve contents from a specific object's attributes into a textview or a button?

Note: I tried following the online documentation for Queries. Their example showed how to retrieve objects by queries, but not how to show those objects in TextViews or how to store those objects inside a local variable. For example, I know how to query for a particular object, but once I do get the object how do I retrieve a particular attribute of that object, how do I get what is stored inside "optionA" field and store it inside a string?

I know there is a ListView adapter, but as you can see I can't use a ListView here. I tried the following code trying to convert the queried objects into strings, but that didn't work. I am really a newbie, so maybe I am doing something stupid. Please help me out.

Upvotes: 1

Views: 7171

Answers (3)

shreyashirday
shreyashirday

Reputation: 900

Try something like:

    query.whereEqualTo("ques",/*enter question number here*/);
       query.findInBackground(new FindCallBack<ParseObject>(){
                 public void done(List<ParseObject> l; ParseException e){
                          if(e == null){
                                 for(int i = 0; i <l.size();i++){
                                       textView.setText(l.get(i).getString("optionA"))
                                     }
                               }
                              else{//handle the error
                                        }
    }

});

Upvotes: 5

Abhimanyu Shekhawat
Abhimanyu Shekhawat

Reputation: 186

See if you wanna import the whole of the database you can proceed with the following code(The class structure has a simple BOY and GIRL records)-

ParseQuery<ParseObject> query = ParseQuery.getQuery("Friends");
            //query.whereEqualTo("Boy", tf3.getText().toString());   //This is to filter things!
            query.findInBackground(new FindCallback<ParseObject>() {
                public void done(List<ParseObject> scoreList, ParseException e) {
                    if (e == null) {
                        int len=scoreList.size();
                        for(int i=0;i<len;i++) {
                            ParseObject p = scoreList.get(i);
                            String boy = p.getString("Boy");
                            String girl = p.getString("Girl");


                        }
                    } else {
                        Log.d("score", "Error: " + e.getMessage());
                    }
                }
            });

Upvotes: 0

Marius Waldal
Marius Waldal

Reputation: 9932

Once you get the object (and you said you know how to), you access the properties like this:

String theQuestion = object.getString("question");

Putting it in a textview would then be like this:

yourTextView.setText(theQuestion);

You can get more info on object access in the Android guide:

https://parse.com/docs/android_guide#objects-retrieving

Upvotes: 1

Related Questions