Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7951799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:40:15+00:00 2026-06-04T02:40:15+00:00

I’m developing an app where I need to get a list of files, read

  • 0

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:

  1. 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.

  2. Use some kind of a sorted array, as mentioned here. I’m not sure how/if I can implement it with an ArrayAdapter.

  3. Forget about using a thread to fill the ListView. Just add a “Loading” message or nothing at all.

  4. 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:40:17+00:00Added an answer on June 4, 2026 at 2:40 am

    You can use a binary search to find an appropriate position for inserting new element.
    As a result, the list is always sorted.

    For example:

    public static void add(List<Integer> list, Integer value) {
        int index = Collections.binarySearch(list, value);
        list.add((index < 0) ? (-index - 1) : index, value);
    }
    
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
    
        add(list, 1);
        add(list, -5);
        add(list, -7);
        add(list, 100);
        add(list, 0);
        add(list, 90);
        add(list, -10);
        add(list, 0);
        add(list, 1);
    
        System.out.print(list);
    }
    

    Then you will get an output like this:

    [-10, -7, -5, 0, 0, 1, 1, 90, 100]

    It works fine. The binary search takes O(log(N)) and the inserting takes O(N) in the worst case (because when you is inserting a new element, can occur the relocating elements in the list). As a result, it takes O(N + log(N)) time. It better than O(N*log(N)) when you sort the list everytime.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.