Android how to save a large list of <String []> during application launch
What is the best way to save List<String[]>
during my application? I have problems with my approach. In most cases, this gives me an OutOfMemory error, since the list is too large.
List<String[]>
is the result of parsing the csv file that I downloaded over the Internet. What I am doing is parsing csv in action and then storing its result in a static member of the class, for example:
String url = "http://xxx/pdf/pms/pms_test.csv"; try { InputStream input = new URL(url).openStream(); CSVReader reader = new CSVReader(new InputStreamReader(input)); SchedController.sched = reader.readAll(); input.close(); }
... then go into ClassName.sched
for different actions.
I do it so that the analyzed data is available in every action ... And I do not need to analyze it again. What can I do to improve it?
I think you can have 2 approaches.
- You save the file and analyze it in a lazy way of loading
- You create a database and save your data.
I suggest you create a database, it is not difficult and will allow you to manage your data well. You can easily execute lazyLoading with the cursor or use ORM ( ORMLite / Greendao ) ... I think this is the best way and the fastest to load your data.
Hope this helps you.
Just add a comment Paresh. I can offer something similar that I used in my application.
For example, I need to display a list of items in the repository. (Each item will have an identifier, name and value).
To achieve this, I first make a request to the server to get the number of items, such as itemCount. Now I have set a limit on the number of items displayed on page 100. If itemCount is greater than 100, I show a warning showing only 100 items, and the next button
can be added to load the next set of items. Or, if itβs a search, you can ask the user to return and refine the search. If itemCount is less than 100, you will not have a problem.
Thus, paging can be implemented to avoid OutOfMemory
problems OutOfMemory