Most efficient way of storing, writing and reading a database of items.
The android-app I'm programming is going to have a database of products, say 150-250 of them. Each item will have properties like name, cost, EAN-number, etc.
My first idea was to store everything in an xml-file which is loaded when the app starts and then populated into an array/map of products. The products need to be viewable in a listview and the user should be able to add more of these products through the app. Most of their properties practically never changes but a few of them do change like all the time. I don't know if it's a good idea to change the properties in both the product-array and the xml and then resave to the xml-file eveytime something changes at the same time. Maybe serialization would be a better option? Though either way i suppose the file wouldn't need to be written to for each cha开发者_JAVA技巧nge, I guess it could be done like when the app closes or on intervals or something(not quite sure on that one either since I' all new to developing mobile-apps and i don't want any of the data to get lost). Maybe it would be a good idea to separate their "static" and "dynamic" properties in some way?
I'm quite lost here. Any suggestion as to what would be a good way to go?
Been searching around on stackoverflow and google but haven't really been able to find something useful.
You should use SQLite based ContentProvider
. This way you will be able to update you products independently from any other component (Activities or Services). All intrested parties can receive notifications when date is changed.
Also there is a support for ContentProviders in ListView (via CursorAdapter
), and you also receive free support from Loaders (via CursorLoader
).
And apart from all that, you have a more efficient way of storing data (i.e. you do not need to re-write xml file on each save). SQLite is going to take care about saving all changes for you in a very efficient and robust way.
Structured data that you will be accessing all the time is better-handled in a SQLite database rather than an XML file. If you go the XML route, you'll have to write all the code to parse, query, store the data. With a database, most of these functions require far less code to write and will most likely be more efficient. There is also less potential for bugs. You also avoid having to think about when to commit changes made to your in-memory XML representation to the actual file.
I strongly suggest you read the article on data storage and also look at the Notepad Tutorial as it demonstrates all the techniques that you would need to create, update, and query a database and integrate it with a ListView
.
It might seem like a lot of research, but your app will be a lot more flexible and bug-free if you use the built-in SQLite rather than roll your own XML-file-backed storage.
精彩评论