1

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.

3 Answers 3

5

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
                                        }
    }

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

1 Comment

oh thank you so much for your solution. i can't stress how much it helped me out! i feel bad that I can't even give you an up vote due to being a new user :(
1

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

2 Comments

Your advice really helped me out... thanks a lot! Now I can fetch and show the data if I am using objectId for searching the object. But if I use any other queries the problem is I get a list instead of a single object, and then I don't know how to address a specific object from that list. Could you help me please?
Yes, you will always get a list in return. Even if the query only results in one object. If you KNOW there is only one object, you can use l.get(l.size()-1); (with l being the list of objects). If there are multiple objects, you need to iterate through them. Like with the for loop in the answer you accepted. (Not sure why you accepted that answer if you still have this problem...?)
0

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());
                    }
                }
            });

Comments

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.