Gyula Soós
Gyula Soós

Reputation: 487

Store methods in separate file. Android

I have small andoid app, which asks the user some questions. The questions are set up in this format:

private void generalKnowledgeQuestions(){
    questions = new ArrayList<>();
    //the first option is the button on the left. if the boolean set to false then the second option is correct
    questions.add(new QuestionObject("Where was the picture taken?", true, R.drawable.cuba, "cuba", "singapore", "the picture has cars in it!"));
    questions.add(new QuestionObject("This city is in which country?", false, R.drawable.barcelona, "Hungary", "Spain", "The beaches of barcelona"));}

And many more of these...

I was wondering if I could put this method in a separate file, for updating the questions more easily, and not having to come inside the activity file where accidents can happen? thanks

Upvotes: 3

Views: 76

Answers (1)

vguzzi
vguzzi

Reputation: 2428

You could create a QuestionUtils class which contains this method in a static context. Like this:

public abstract class QuestionUtils {

public static ArrayList<Question> generalKnowledgeQuestions(){
       ArrayList<Question> questions = new ArrayList<>();
       //the first option is the button on the left. if the boolean set to false then the second option is correct
       questions.add(new QuestionObject("Where was the picture taken?", true, R.drawable.cuba, "cuba", "singapore", "the picture has cars in it!"));
       questions.add(new QuestionObject("This city is in which country?", false, R.drawable.barcelona, "Hungary", "Spain", "The beaches of barcelona"));

       return questions;
   }
}

I would recommend keeping your question String data inside an xml file.

As requested to reference this method from another class you would simply have to call QuestionUtils.generalKnowledgeQuestions(); The method would return an Array of your questions to you, I don't know what you have called your question Object so change Question to whatever you are using.

Upvotes: 1

Related Questions