将方法存储在单独的文件中Android的

时间:2015-11-19 11:12:31

标签: android

我有一个小型的andoid应用程序,它会询问用户一些问题。问题以这种格式设置:

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

还有更多......

我想知道我是否可以将此方法放在单独的文件中,以便更轻松地更新问题,而不必进入可能发生事故的活动文件中? 感谢

1 个答案:

答案 0 :(得分:1)

您可以创建一个QuestionUtils类,该类在静态上下文中包含此方法。像这样:

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

我建议您将问题String数据保存在xml文件中。

根据要求从其他课程引用此方法,您只需致电QuestionUtils.generalKnowledgeQuestions();该方法会向您提出Array个问题,我不知道您的问题是什么Object所以将Question更改为您正在使用的任何内容。