Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
Take your Android programming skills to the next level with the Android built-in framework that enables local data management in text files and SQLite-based relational databases. This course shows you how to create datacentric apps for Android devices, using SQLite, Java, and the built-in android.database packages. Author David Gassner describes how to define shared preferences, work with JSON and XML files in internal and external data stores, and create new local SQLite databases.
I've previously described how to read data from an XML file, showing you two different parsers that you can use, and I've also described how to create new rows in an SQLite database table. I'm going to combine these two features. I'm going to use an XML file to seed my application with data and then move the data over to the SQLite database as the application launches for the first time. I'll be doing all of the work in the main activity class in this version of my project, ImportToDb.
In the main activity class down at the bottom of the class I have a method called Create data, and right now I'm creating some fake data. I'd like to replace this code with code that imports from XML and exports to the database. So I'm just going to select and delete all of this code. If you need this code back again, you can find it in the previous version of the project in the Solutions folder. I'll go get code to import data from an XML file, I'll move back up to the On Create method, and I'll locate this code that I commented out in a previous exercise.
It uses the XML Pull Parser to read the data from an XML file and returns it as a list of tour objects. I'll cut that code to the clipboard, then I'll go back down to my Create Data method, and I'll paste the code back in and then I'll uncomment it. You might need to do a quick fix to add an import statement for the ToursPullParser class. So now I have the tours from the XML file in memory, and I can loop through and easily call the methods in my data source object to pass the data to my SQLite database.
I'll use a foreach loop to loop through the list of tours. I'll type foreach, all one word, I'll press Ctrl+Spacebar, and choose the foreach code template. Eclipse automatically picks up that it's going to loop through the tours collection and create a tour object for each item in the collection. So I'll Tab a few times, and now my cursor is within the for loop. Within the for loop, I now have a tour object, and all I need to do is call the Create method of my data source class.
Remember, when I created the data source class which is in the db package, it receives a tour object and it adds it to the database table as a new row. So I'll go back to the activity and I'll simply call it datasource.create and I'll pass in the tour object. And that's it. I'm pulling the data from the XML file and passing it to the database. Everything else in this application will remain exactly as it was before. Up above, in the Uncreate method I'm doing a findAll to get the data from the tour's table, then I'm checking the size, and if it's 0, I'm calling this createData that I just modified to pass the data in, and then I'm retrieving the data again.
I'll save my changes. Now before I test this, I'm going to go to the emulator and clear all of the existing data to make sure I'm getting a fresh start. I'll go to the emulator and then I'll go to the Applications list. I'll click and hold on my application icon, I'll drag it up to App info, and then after a moment the emulator will let me clear the data. Then I'll go back to the Home screen, I'll return to Eclipse, and I'll run the application in the emulator again.
Now as the application opens in the emulator, it will query the tour's table. It will determine that there isn't any data in the table. It'll then call the createData method. It will retrieve the data from the XML file, pass it into the database, requery the database, and then display the data. So now you have a good example of how you can seed an application with data that you package up as an application resource. To review a couple of things from previous lessons, my XML file is a resource in the resources folder.
I placed it in a folder called raw, and it's a file called tours.xml. By putting it in the Resources folder, you make it a resource that you can address with the Resource ID, just like a drawable image, just like a menu item, or a user interface control. As another reminder, I'm using this class, ToursPullParser. But if you prefer, you can use the JDOMParser. Either one will do the job, and which one you use is a matter of taste, depending on how you like to do your coding and also want a performance because the two parsers return different results depending on the nature of the XML and the context of your application.
Finally, in the MainActivity I've added simple logic to determine whether there's data in the Application already, and if not, I've added code to move the data from XML to the database.
There are currently no FAQs about Android SDK: Local Data Storage.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.