0

I'm implementing an Android quiz in Java where user is shown a random image and is asked to choose what he can see in it. The images are in /res/drawable, however I'm wondering how to bind the image name and a few possible types of answers to the given image, each indicating something different about the person taking the test.

My first idea was to create a Java class serving this purpose:

public class Question {
    Question(String imgSrc, String ans1, String ans2, String ans3)
    {
        imageSource = imgSrc;
        answer1 = ans1;
        answer2 = ans2;
        answer3 = ans3;
    }

    private String imageSource;
    private String answer1;
    private String answer2;
    private String answer3;
}

and then another class QuestionSet where specific questions could be constructed and stored as public static objects accessible from QuestionActivity. However, I'm reading that for some reasons static Java objects should be avoided in Android programs (as is explained here), especially when they are to last in memory for a long time as is the case with my quiz.

So my question is what the best alternative for my current approach could be.

Theta
  • 231
  • 2
  • 14
  • Just don't make it static. Put it in a regular class which is instantiated when needed. Although if you intend on having lots of data, putting it in some sort of database would be best. – Nicolas Oct 15 '20 at 13:04
  • @Nicolas The more I think about, the more advantages of using a database I see. Instantiation of scoped objects whenever needed probably won't work because I have to store member values somewhere anyway. And I don't want to run the risk of static objects so it seems database is the way to go. – Theta Oct 15 '20 at 14:55
  • 1
    You can look into prepopulated databases then. Room should work, but there are other alternatives too. – Nicolas Oct 15 '20 at 14:57
  • @Nicolas Thanks for the suggestion, Room is new to me but at first glance it looks great. – Theta Oct 15 '20 at 15:19

0 Answers0