Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to fill an array asynchronously in a sorted way in Java?
    primarykey
    data
    text
    <p>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.</p> <p>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 <code>ArrayAdapter</code> 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:</p> <ol> <li><p>I thought of sorting the list every time I insert an item, but that would make the process even slower... But again, there's <a href="https://stackoverflow.com/questions/1390832/how-to-sort-nearly-sorted-array-in-the-fastest-time-possible-java">this</a>, but I'm not sure I understand how to use such a sorting algorithm.</p></li> <li><p>Use some kind of a sorted array, as mentioned <a href="https://stackoverflow.com/questions/4031572/sorted-array-list-in-java">here</a>. I'm not sure how/if I can implement it with an <code>ArrayAdapter</code>.</p></li> <li><p>Forget about using a thread to fill the <code>ListView</code>. Just add a "Loading" message or nothing at all.</p></li> <li><p>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...</p></li> </ol> <pre> 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(); </pre> <p>I need your feedback on which solution I should use, and how to use the first two (if I should use them)?</p> <p>Thanks a lot.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload