Populate SQLite using XML and integrating inside Activity Populate SQLite using XML and integrating inside Activity sqlite sqlite

Populate SQLite using XML and integrating inside Activity


createDataBase() method should be called inside onCreate() method of XMLtoSQLite class


You should not put them inside an xml file.

Reason one : For the performanceReason two : For the clarity of your code

Usefull link : http://www.vogella.com/articles/AndroidSQLite/article.html

Architechture example :

AnObject.java file that represent a single table (onCreate and onUpdate method should be implemented custom here, toString(), ...)AnObjectManger.java file that manage that table, do the request, ..AnOpenHelperDataBase.java that simplify the use of sqlite and do the basics operations

And note that to create the database you only need this example:

public DatabaseOpenHelper(Context context, CursorFactory factory) {        super(context, DATA_BASE_NAME, factory, DATABASE_VERSION);    }

EDIT :

If you still want to keep your approach here is the order of operation

1 - Download the data

2 - Create de database and the tables empty

3 - Fill the tables

4 - Get the data you need in a simple cache (List, HashMap, ArrayList, ...)

5 - Display/use those data at the moment you need it if it is available

NOTE THAT :

1 - Downloading should be done in another thread (Thread, Asynctask, service ...)

2 - Operations on the database should generally also be done in another thread too


I think that the best way for you is to follow some useful guides. Let's split the problem into more simple parts:

Database

IMHO you have to well create your database class, so follow Vogella's guide; it's very useful and explain in detail everything about database.

Downloading your data from web

In the future (see comments above) you will need to download your data from web, so use AsyncTask class in order to connect to a site and then download all you need; if you don't know this class, read this official guide.

Parsing XML

You have to parse your XML: you can use this guide, or this one. I use DocumentBuilder, but my XML files are different from yours:

...String errorCode = null;Document changes;try {    // URL were lies some xml-data    URL url = new URL(params[0]);    // Creating a document    InputSource is = new InputSource(url.openStream());    DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();    changes = db.parse(is);    changes.getDocumentElement().normalize();    // Searching AUTHKEY in created document    NodeList root = changes.getElementsByTagName("Auth");    Element myRoot = (Element) root.item(0);    errorCode = myRoot.getAttribute("ErrorCode");    auth = myRoot.getAttribute("Key");} catch (IOException e) {    errorCode = null;} catch (ParserConfigurationException e) {    errorCode = null;} catch (SAXException e) {    errorCode = null;} catch (Exception e) {    errorCode = null;}...

I hope that you will find useful every link.

EDIT

On your MainActivity add this (of course after you have changed your code):

@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    String url = "http://myUrlWhereDataIsStored.com";    // Creating database    MyDatabase database = new MyDatabase(this);    // Populating database    new MyAsyncTask().execute(url);}