0

I have a large CSV file (about 12000 lines) that I need to parse and display in a ListView.

I don't want to load the entire file in memory, I want to read it in 10 lines groups and when the user clicks a "Load more" button, the next 10 lines are read and so on.

any recommendations on how to achieve this in an efficient way ?

Thanks

Mina Wissa
  • 10,923
  • 13
  • 90
  • 158
  • "I want to read it in 10 lines groups and when the user clicks a "Load more" button, the next 10 lines are read and so on" -- why do you think that is what the *user* will want? – CommonsWare Apr 06 '14 at 15:52
  • What I would do: **1** - (On the first application launch - you may retain this info in a preference key) Store the CSV in a db, reading it line by line and inserting it. **2** - retrieve data in "pages" of 10 records and fill an adapter for the ListView – Phantômaxx Apr 06 '14 at 15:52
  • @CommonsWare because that's already a requirement from the client :) – Mina Wissa Apr 06 '14 at 15:53
  • @Vyger is there any other method than storing the file in a SQLite db ? – Mina Wissa Apr 06 '14 at 15:54
  • 1
    So your client wants the user to have to press "Load more" 1,200 times? Does your client actually have a functioning brain? – CommonsWare Apr 06 '14 at 15:56
  • I think that using a db is the preferred (fastest) way to do that. Or you could read the file "every n*10 lines" (well, in "chunks of 10 lines"), if dbs aren't really your strenght. – Phantômaxx Apr 06 '14 at 15:57
  • It's just that there is a search functionality, some kind of filter on the fields of the CSV file, so he assumes that the user can see some items from the file using the "Load more" button, and search the file based on some criteria. – Mina Wissa Apr 06 '14 at 15:58
  • 2
    That would be all the more reason to use a SQLite database, as you cannot efficiently search a CSV file. – CommonsWare Apr 06 '14 at 16:01
  • @Vyger that will be nice, but how do I mark that I've read 10 lines when reading the next lines, how can I start reading from an specific location/line from the file ? – Mina Wissa Apr 06 '14 at 16:01
  • There's an answer to this question here: http://stackoverflow.com/a/2138411/2649012 – Phantômaxx Apr 06 '14 at 16:03
  • Thanks @CommonsWare of course SQLite db will be the best approach, I'm just thinking loudly about solving this without using SQLite – Mina Wissa Apr 06 '14 at 16:03
  • @Vyger Unfortunately, they use the Apache Commons library, no APIs from the Android SDK, anyway I'll check if we can use the library with Android. Thanks – Mina Wissa Apr 06 '14 at 16:08

1 Answers1

1

Well, here's the likely starting point:

Step #1: Create an ArrayAdapter, whose data model is your array of read-in rows

Step #2: Override getCount() to return the array size plus 1, to accommodate your extra "Load more" row

Step #3: Override getViewTypeCount() to return 2 (or more, if your ListView rows for the CSV rows are not all the same -- I will assume they are all the same for the rest of this answer, so the two types are "regular row" and "'Load more' row")

Step #4: Override getItemViewType() to return 1 when the position is equal to your array size, or 0 for everything lower than that

Step #5: Override getView() to return the appropriate row, including returning your "Load more" row

Step #6: When the user clicks the "Load more" row, run through your logic to read in the next CSV lines, adding them to your array, then calling notifyDataSetChanged() on the adapter to alert the ListView that the data has changed

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks a lot, now how can I keep track of the read lines from my file, so the next time I read the next 10 lines, I don't have to go through the previous ones ? – Mina Wissa Apr 06 '14 at 16:14
  • @MinaSamy: That would depend a lot on how you are reading in the CSV file. As I do not think that I have read in a CSV file in Java in a decade, I cannot really help you with that. Note, per a comment on your question, that Android has no particular support for CSV, the way it does for JSON and XML. Hence, however you read in the CSV file is likely to be general-purpose Java, nothing particularly Android-specific. – CommonsWare Apr 06 '14 at 16:21