someone new someone new - 1 year ago 74
Java Question

Android: What data structure to use to save app's data

So I have a simple android app which is like this: It has a list of Car Manufacturers such as Honda, Ford, Mazda etc.
Then when one of the manufacturer is chosen, I need to display their car models. So if user selects Honda, I would display "Civic, CRV, Odyssey" etc.
And when user selects a specific model, I would display another list with more information.
So what kinda of data structure would be simple and easy to implement in my android app. I was thinking about using nested ArrayLists.
Also, can you please provide a link to an example on how to save the data internally using android studio and get the data when I need to.

Note: The list is dynamic, meaning that the user will have the option to add or delete any item from any of lists.

Thanks :)

Answer Source

This is practically what databases are for (of course I'm talking about SQLite here). I'm assuming you will have loads of data that are interconnected in a way.

I do not know how well you know database design, but you should have a table called Manufacturer which will hold the data about those (think about what data you want to save and therefore create proper columns of the table) and a table called CarModel which will have a foreign key which connects it to the Manufacturer (called manufacturer_id). These two tables need to have Many-To-One relationship -> Each manufacturer can have Many models, but each model can be produced by only One manufacturer.

This is just a basic idea which you can still improve to your own liking, but this should cover everything that you asked for very easily. For example, to get a list of Manufacturers, you'd simply Select * from Manufacturer. When you click a particular manufacturer and wanna display it's models, you'd just write a quite simple query Select * from CarModel where manufacturer_id = id (id being the parameter you pass to some function, the id of the manufacturer you want the models for. The models you select can have arbitrary amount of data like additional information about that model.

If you're not that good with raw queries, there are useful query builder which can make your life a whole lot easier.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download