5

I have like 5000 names in the database. I want all these names to be inflated onto a ListView. Which has the following elements

  1. Icon Image (which is stored locally in Drawables)
  2. Name
  3. Distance in kms

I am filtering this listView using Search Filtering, something like this:

adapter.getFilter().filter(someText);

I am also sorting the listview, for example: sort listView names alphabetically(A-Z and Z-A). Sorting is done on the listView adapter like this:

adapter.sort(new Comparator<String>() {

                @Override
                public int compare(String lhs, String rhs) {
                    return lhs.getPlaceName().compareTo(rhs.getPlaceName());
                };
            });

Now i am pretty confused whether to use Lazy loading of names onto the listview(because I have 5000+ names) considering the performance of the adapter. Please suggest.

Harsha M V
  • 54,075
  • 125
  • 354
  • 529
suresh cheemalamudi
  • 6,190
  • 11
  • 49
  • 67
  • 4
    Since it is a database anyway, use a Cursor? As far as I know, this does lazy loading by itself. – NickL Jan 08 '13 at 06:40
  • 1
    Paging is recommended in such a case (Play store app for example). Lazy-Load first 100 or so, and provide _load next 100_ or _load previous 100_ actions (may-be when user scroll to top or end). Defer filtering to your query. – S.D. Jan 08 '13 at 06:44
  • @NickL I am using stORM:An Open Source ORM for SQLite on Android by David M Chandler, which will automate my database transactions and What i have acccess to is only Data access object.ie (i can do only insert, delete, update on it). I am not sure whether i can use cursor on that.? – suresh cheemalamudi Jan 08 '13 at 06:44
  • 1
    @Singularity can you share more information on Paging in listview Android ? – Harsha M V Jan 08 '13 at 06:47
  • @Singularity play store apps are retreived from url and displayed. But in my case i have everything locally available(ie, place names, distance and icon images as well). So how will I be benefited on using paging.? – suresh cheemalamudi Jan 08 '13 at 06:57
  • 1
    @sureshcheemalamudi Not sure if you can use the cursor on the framework you are using, but even then you can still use the Cursor on the SQLite database itself? Cursor does not need ORM. – NickL Jan 08 '13 at 07:28

1 Answers1

2

Alternatively, you may store your data in the database sorted and then apply lazy loading. Because though approach suggested by @Singularity in very good, you may end up sorting only chunks [of 100, say] and not the whole data. Also, you would require sorting to be done for each of those chunk.

HarshalK
  • 126
  • 5
  • what i actually need is, whether i am performing searching or sorting, it should apply for all the 5000+ items at once. If i do lazy loading can i achieve this.? – suresh cheemalamudi Jan 08 '13 at 09:53
  • Also storing the sorted data to database and retrieving it back may be a huge work on thread i guess.? – suresh cheemalamudi Jan 08 '13 at 09:56
  • 1
    Hi Suresh, lazy loading means loading items as and when required. So I think that will not solve your purpose if you want sorting to be applied for all 5000. Also, as per ur orignal ques, I assumed that you have a static database which is not building upon at run time. Hence the suggestion for sorting it before hand. If you are not adding items to ur db at run time then you can sort your table beforehand and then may be you can take advantage of paging as suggested by @Singularity. Let me know if that helps. – HarshalK Jan 08 '13 at 10:11
  • thanks for your suggest. I am dumping the values to the database via XML parsing. once the values are dumped, i will not insert any more values as of now. What i am doing is just populate these values onto the listview and perform search and sorting as and then required. – suresh cheemalamudi Jan 08 '13 at 10:23
  • 1
    ok. So may be u can then consider insertion sort [inserting items in ur db in sorted order] while parsing.The bottom line is, if u want to use lazy loading [pagination]which is recommended in ur case, then u may have to consider sorting the data first. IMO, since you have to compulsorily sort the data whether u use the lazy loading or not, why not go ahead and use that and ensure better responsiveness of ur UI. – HarshalK Jan 08 '13 at 11:53
  • am sorry if am not clear, i will interact with the databse only to fetch items to populate the listview. But for sorting nad searching i will only deal with adapter. ie, i will sort the adapter items and perform nofitydatasetchanged. – suresh cheemalamudi Jan 08 '13 at 13:05