开发者

What to do with XML information after first startup of Android App

开发者 https://www.devze.com 2023-03-10 05:12 出处:网络
So im looking for some guidance in terms of storing data within 开发者_如何转开发my android application.

So im looking for some guidance in terms of storing data within 开发者_如何转开发my android application.

At the moment, i have an XML file that stores a bunch of Restaurant elements. i read this file when the application starts and store all the restaurants in an ArrayList.

Now i am trying to add a favorite Restaurant functionality. The only thing is, how would i store the favorite Restaurant.

Should the application read the information from the XML file and then place it into a database , or should i give an id to every Restaurant and then store their favorite Restaurant's inside the database.


Store all restaurant data in a database and make sure you have a field "isFavorite" or similar.


XML-files are nice if you want to include data that doesn't change. If you want to work with your data and change it, a Database is the common way.

But do it with an SQLiteOpenHelper, where you can also define some standard-values in the onCreate-method.


The first time your App starts (after the user installed it) and you want to access your Database through your SQLiteOpenHelper, the onCreate-method is called. In this method, you'll want to create your needed Database-tables.

If you want some standard data in your Database, this is also the place to add these (using some inserts).

After that, SQLite has created a database (which is actually a file in the applications /databases-directory), the Database exists (and can be used) as long as this file is there (which should be till the user deletes the app or your app deletes the Database). So you have a persistent data-storage. So you don't need an XML-file at all.

If you however need to get your Data from a XML-file (like from the Web), you can use a XML-parser to get your informations from the XML-file and put them in the Database using PreparedStatement's.

0

精彩评论

暂无评论...
验证码 换一张
取 消