4

I am making Android app for practicing driving licence theory tests. I will have about 3000 questions. Question object would have several atributes (text, category, subcategory, answers, group). I will create them and put in app, so data won't ever change. When user chooses category, app would go througt data, look which question meets requirements (that user selected) and put it in list for displaying. What should I use to store data/questions, XML or SQLite? Thanks in advance.

Edit: I forgot to mentiont that app won't use internet connection. Also, I planned to make simple java app for entering data. I would copy text from government's website (I don't have access to their database and I have to create mine), so I thought to just put question's image url to java program and it would download it and name it automaticaly. Also, when entering new question's text it would tell me if that question already exist before I enter other data. That would save me time, I wouldn't have to save every picture and name it my self. That is what I thought if using XML. Can I do this for JSON or SQLite?

5 Answers 5

4

If you do not have to perform complex queries, I would recommend to store your datas in json since very well integrated in android apps using a lib such as GSON or Jackson.

If you don't want to rebuild your app / redeploy on every question changes. You can imagine to have a small webserver (apache, nginx, tomcat) that serves the json file that you will request on loading of the app. So that you will download the questions when your app is online or use the cached one.

XML is a verbose format for such an usage, and does not bring much functions....

To respond to your last question, you can organise your code like that :

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


    }

}

CONSOLE OUTPUT

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 : provides you the complete working code I've made for you https://gist.github.com/jeorfevre/5d8cbf352784042c7a7b4975fc321466

To conclude, what is a good practice to work with JSON is : 1) create a bean in order to build your json (see my example here) 2) build your json and store it in a file for example 3) Using android load your json from the file to the bean (you have it in andrdoid) 4) use the bean to build your form...etc (and not the json text file) :D

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for answering, I will take a look at JSON. I edited my post, can you read edit and tell me if that affects your answer?
updated my response, in order to show you how to fill a bean, how to create a json string. :) enjoy
Thank you very very much. I will examine and test the code when I get home. When I test everything I will mark your answer.
It works allright. I will expand it and make it suit my app. Thank you very much for quick help! :)
1

I would recommend a database (SQLite) as it provides superior filtering functionality over xml.

Comments

1

Create the db using DB Browser for SQLite

And then use the library SQLiteAssetHelper in the link- https://github.com/jgilfelt/android-sqlite-asset-helper

Tutorial on how to use - http://www.javahelps.com/2015/04/import-and-use-external-database-in.html

Comments

1

You can use Paper https://github.com/pilgr/Paper its a fast NoSQL data storage for Android.

1 Comment

never tryed it, wil give a try. thanks for sharing.
1

SQLite is the best for your system. because you will have to maintain (text, category, subcategory, answers, group) etc. So if you create db and create table for them. That will be easy to manage and you can relationship with each other which is not possible to XML.

1 Comment

Thank you for answering. Questions won't change at all. Only good thing with sqlite that I see is tables, which can be used to separate categories. I edited my post, can you read edit and tell me if that affects your answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.