I have a simplistic app (just learning) that reads some XML data from a mocked up file. The XML data is well formed into 6 categories and I use the SAX parser to read it. My app basically has two buttons prev & next. So when the app loads I’d like to see the first category of XML data. When the user presses next button… well then I’d like to see the next category of data etc. to the end.
My question is how do I go back and forth through the data? Do I load it all into a data object with some form of sorting and iterate back and forth through the object or do I add an atty field to a parent element and just search the XML for the requested atty and child data? I don’t foresee the XML ever getting very large. Just trying to get more experienced input into how to go about synchronizing the data with the gui.
There are many ways you could go about it. One that is generally a decent path is parse the XML into a data structure that can be used by an Adapter to create the view structures and return them so be shown. That will give you a good level of control over how your data looks and allow you to tie in to many different complex View structures pretty easily.
The data structure that you store it in also has many possibilities. Which ones work best would depend on your particular dataset generally.
Given what I know about your data an ArrayList seems like a straightforward approach. Create yourself a class that will hold all of the data about one category. Create objects of that class in your parser as you are pulling the data out of the XML file, each time you get to a new category add your object to an ArrayList. When your done you should have an List structure that has 1 category object(with all of its data) at each index.
Once you’ve got that set up make yourself an ArrayAdapter with your List. Override the getView() method to inflate your View objects and populate them with the data from your List.
This Adapter can then feed a parent View (ViewPager, ViewSwitcher, ListView etc…) These parent views will make it easy to iterate over your data structures (i.e. switching from one category to the next and back.)