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

Android 将方法存储在单独的文件中。安卓,android,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

我有一个小的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"));}
private void GeneralKnowledgeRequests(){
问题=新的ArrayList();
//第一个选项是左边的按钮。如果布尔值设置为false,则第二个选项是正确的
问题。添加(新的问题对象(“照片在哪里拍的?”,true,R.drawable.cuba,“古巴”,“新加坡”,“照片里有汽车!”);
问题。添加(新的问题对象(“这个城市在哪个国家?”,false,R.drawable.barcelona,“匈牙利”,“西班牙”,“巴塞罗那海滩”);)
还有更多的

我想知道我是否可以把这个方法放在一个单独的文件中,以便更容易地更新问题,而不必进入可能发生事故的活动文件中?
谢谢

您可以创建一个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;
   }
}
公共抽象类问题{
公共静态ArrayList GeneralKnowledgeRequests(){
ArrayList问题=新建ArrayList();
//第一个选项是左边的按钮。如果布尔值设置为false,则第二个选项是正确的
问题。添加(新的问题对象(“照片在哪里拍的?”,true,R.drawable.cuba,“古巴”,“新加坡”,“照片里有汽车!”);
问题。添加(新问题对象(“该城市在哪个国家?”,假,R.drawable.barcelona,“匈牙利”、“西班牙”、“巴塞罗那海滩”);
回答问题;
}
}
我建议将您的问题
字符串
数据保存在xml文件中


根据从另一个类引用此方法的请求,您只需调用
QuestionUtils.GeneralKnowledgeRequests()
该方法将返回一个
数组
你的问题给你,我不知道你把你的问题称为
对象
所以把
问题
改成你正在使用的任何东西。

你可以使用一个文本文件,然后使用该文本文件是的,我在想类似的事情,唯一的事情,我不知道如何:Dyou可以创建一个XML文件并将所有数据保存在那里,或者保存任何json文件。有很多方法可以做到这一点。制作一个文本文件(html格式)并放入assets文件夹,然后使用StringBuilderHanks。我很感激您花了这么多时间来回答,但是我不能使用它,因为我遇到了一些错误,我不知道如何解决它们,也不知道接下来的步骤,如何在我现有的类中使用这个类……xml文件也是一个好主意,但同样,我不知道如何解决。不过我会继续用谷歌搜索Dif如果有帮助,下面是完整的项目:谢谢!:你帮了很多忙D