I'm developing an app where I need to get a list of files, read some data from it, and display all of these files, sorted based on that data, in a ListView.
The problem is that there might be plenty of them, so it takes time to load them all. I had a choice to either load them asynchronously (in a thread), or display a loading box. I am having a problem with the first one: the ArrayAdapter
is being filled, then sorted in the end, so by the time all the items are there, the list is not sorted. The solutions I thought of are:
I thought of sorting the list every time I insert an item, but that would make the process even slower... But again, there's this, but I'm not sure I understand how to use such a sorting algorithm.
Use some kind of a sorted array, as mentioned here. I'm not sure how/if I can implement it with an
ArrayAdapter
.Forget about using a thread to fill the
ListView
. Just add a "Loading" message or nothing at all.Store the data in the files in a database, with the path of the file stored, and read all the entries from the database. But I'm not sure this will make the process faster...
la = new ArrayAdapter(this, R.layout.list_item); setListAdapter(la); Handler handler = new Handler() { public void handleMessage(Message message) { switch (message.what) { case TrackBrowser.DID_SUCCEED: { // This is called when the thread is done finding the list of items // mComparator is my own Comparator la.sort(mComparator); break; } case TrackBrowser.ADD: { // This is called everytime an item is parsed TrackBrowser.TrackView tv = (TrackBrowser.TrackView) message.obj; la.add(tv); // Should I sort here everytime? //la.sort(mComparator); break; } } } }; // This class just loops through the files returned by listFiles and sends messages. TrackBrowser tb = new TrackBrowser(handler); Thread thread = new Thread(tb); thread.start();
I need your feedback on which solution I should use, and how to use the first two (if I should use them)?
Thanks a lot.