安卓——静态数据使用XML还是SQLite?

4
我正在制作一款用于练习驾照理论测试的Android应用。我将准备约3000道题目。每道题目对象都将拥有多个属性(文本、类别、子类别、答案、组)。我将创建它们并放入应用程序中,所以数据将不会发生变化。当用户选择类别时,应用程序会浏览数据,查找符合要求(用户选择的)的问题,并将其放入列表以便显示。我应该使用XML还是SQLite来存储数据/问题?谢谢。
编辑: 我忘记提到这个应用程序不会使用互联网连接。此外,我计划制作一个简单的Java应用程序用于输入数据。我将从政府网站复制文本(我无法访问他们的数据库,因此必须创建自己的数据库),因此我想只需将问题的图像URL放入Java程序中,程序就会自动下载并命名它。此外,在输入新问题文本时,它会告诉我是否已经存在该问题,这样我就不必保存每个图片并自行命名它。如果使用XML,我认为可以这样做。我是否也可以在JSON或SQLite中实现这一点?
5个回答

4
如果您不需要执行复杂的查询,我建议您将数据存储在json中,因为它可以很好地与Android应用程序集成,使用像GSONJackson这样的库。

如果您不想在每次问题更改时重新构建应用程序/重新部署,请考虑拥有一个小型Web服务器(如apache,nginx,tomcat),该服务器提供您在应用程序加载时请求的JSON文件。因此,您将在应用程序在线时下载问题或使用缓存的问题。

XML是一种冗长的格式,对于这种用途没有太多功能......

为了回答您的最后一个问题,您可以像这样组织您的代码:

/**
 * SOF POST http://stackoverflow.com/posts/37078005
 * @author Jean-Emmanuel
 * @company RIZZE
 */

public class SOF_37078005 {

    @Test
    public void test() {
        QuestionsBean questions = new QuestionsBean();

        //fill you questions
        QuestionBean b=buildQuestionExemple();
        questions.add(b); // success
        questions.add(b); //skipped     

        System.out.println(questions.toJson()); //toJson
    }


    private QuestionBean buildQuestionExemple() {
        QuestionBean  b= new QuestionBean();
        b.title="What is the size of your boat?";
        b.pictures.add("/res/images/boatSize.jpg");
        b.order= 1;
        return b;       
    }


    public class QuestionsBean{     
        private List<QuestionBean> list = new ArrayList<QuestionBean>();

        public QuestionsBean add(QuestionBean b ){
            if(b!=null && b.title!=null){
                for(QuestionBean i : list){
                    if(i.title.compareToIgnoreCase(b.title)==0){
                        System.out.println("Question "+b.title+" already exists - skipped  & not added");
                        return this;
                    }
                }
                System.out.println("Question "+b.title+" added");
                list.add(b);            
            }
            else{
                System.out.println("Question was null / not added");
            }
            return this;
        }
        public String toJson() {
            ObjectMapper m = new ObjectMapper();
            m.configure(Feature.ALLOW_SINGLE_QUOTES, true);
            String j = null;
            try {
                j= m.writeValueAsString(list);

            } catch (JsonProcessingException e) {
                e.printStackTrace();
                System.out.println("JSON Format error:"+ e.getMessage());
            }   
            return j;
        }


    }

    public class QuestionBean{      
        private int order;
        private String title;
        private List<String> pictures= new ArrayList<String>(); //path to picture 
        private List<String> responseChoice = new ArrayList<String>(); //list of possible choices


        public int getOrder() {
            return order;
        }
        public void setOrder(int order) {
            this.order = order;
        }
        public String getTitle() {
            return title;
        }
        public void setTitle(String title) {
            this.title = title;
        }
        public List<String> getPictures() {
            return pictures;
        }
        public void setPictures(List<String> pictures) {
            this.pictures = pictures;
        }
        public List<String> getResponseChoice() {
            return responseChoice;
        }
        public void setResponseChoice(List<String> responseChoice) {
            this.responseChoice = responseChoice;
        }


    }

}

控制台输出

Question What is the size of your boat? added 
Question What is the  size of your boat? already exists - skipped  & not added
[{"order":1,"title":"What is the size of your boat?","pictures":["/res/images/boatSize.jpg"],"responseChoice":[]}]

GIST:提供了我为您制作的完整工作代码。 https://gist.github.com/jeorfevre/5d8cbf352784042c7a7b4975fc321466

总之,使用JSON的好方法是: 1)创建一个bean以构建您的json(请参见我的示例) 2)构建您的json并将其存储在文件中 3)使用Android从文件加载您的json到bean中(您已经有它在Android中) 4)使用bean来构建您的表单等(而不是json文本文件):D


谢谢您的回答,我会看一下JSON。我编辑了我的帖子,您能读一下编辑并告诉我是否影响了您的答案吗? - VPetrovic
1
更新了我的回复,以便展示给您如何填充一个Bean对象,如何创建一个JSON字符串。:) 享受吧 - jeorfevre
非常感谢你。我将在回家后检查和测试代码。当我测试一切后,我会标记你的答案。 - VPetrovic
它运行得很好。我会扩展它并使其适合我的应用程序。非常感谢您的快速帮助! :) - VPetrovic

1
我建议使用数据库(SQLite),因为它提供比XML更强大的过滤功能。


1

从未尝试过,会尝试一下。谢谢分享。 - jeorfevre

1

SQLite是您系统的最佳选择。因为您需要维护(文本、类别、子类别、答案、组)等内容。因此,如果您为它们创建数据库并创建表格,那么管理将变得容易,并且您可以建立彼此之间的关系,这在XML中是不可能的。


感谢您的回答。问题不会改变。我认为SQLite唯一好的地方就是可以使用表格来分离类别。我编辑了我的帖子,您能看到编辑内容并告诉我这是否影响了您的答案吗? - VPetrovic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接