Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android how to keep large List<String[]> while application runs

What is the best way to keep a List<String[]> while my application runs? I am having problems with my approach. It most of the time gives me an OutOfMemory Error since the list is too big.

The List<String[]> is the result of parsing a csv file that I have downloaded online. What I do is parse the csv in an activity then save its result in a static class member like:

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 access ClassName.sched on different activities.

I am doing this so that the parsed data will be available in every activity... And I don't have to parse again. What can I do to improve it?

like image 676
yojoannn Avatar asked Jul 24 '12 09:07

yojoannn


2 Answers

I think you can have 2 approaches.

  1. You save the file and parse it in a lazy loading way
  2. You create a database and save your data.

I suggest you to create a database, this is not difficult and let you to manage well your data. You can do easily lazyLoading with cursor, or use a ORM (ORMLite / Greendao)... I think this is the best way and the fastest to load your data.

Hope this will help you.

like image 192
AMerle Avatar answered Nov 08 '22 06:11

AMerle


Just to add to Paresh's comment. I can suggest something similar I used in my app.

For example I need to display list of Items in a store. (Each item will have ID, name and cost).

To achieve this I first make a request to server to get number of items, say itemCount. Now I set a limit to number of items displayed in a page say 100. If the itemCount is greater than 100, I display an alert saying only 100 items will be displayed and a next button can be added to download next set of items. Or if it is a search you can ask user to go back and refine the search. If itemCount is less than 100 then you will not have any issues

This way Paging can be implemented to avoid OutOfMemory issues

like image 37
Abhilasha Avatar answered Nov 08 '22 06:11

Abhilasha