Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - XML or SQLite for static data

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?

like image 477
VPetrovic Avatar asked Feb 08 '23 02:02

VPetrovic


2 Answers

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

like image 164
jeorfevre Avatar answered Feb 09 '23 14:02

jeorfevre


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

like image 23
F43nd1r Avatar answered Feb 09 '23 15:02

F43nd1r