简体   繁体   中英

Displaying progress dialog before show content

     progressDialog = ProgressDialog.show(MainActivity.this, "", "Loading...");
     Thread thread = new Thread() {

        public void run() {
            latch.countDown();
            try{
                sleep(3000);        
                } 
            catch (Exception e) {       
                Log.e("tag", e.getMessage());       
            }

            progressDialog.dismiss();

        }

    };
    thread.start();   listView.setAdapter(adapter);

I want show loading in 3 seconds then show listview but list always show immediately. How can I want thread finished before showing listview?

You shouldn't make people wait for no reason. If you have something to load, load it. Otherwise draw what you can when you can.

As an answer to your specific question you would need to have the Thread setAdapter after it slept for 3 seconds (right now that call is outside the Thread... I think you think that that call blocks, but that's the whole point of the Thread, it does its own thing while the rest of your code executes).

However, Thread can't do anything to your display thread (other than throw an exception) so you'd have to wrap that listView.setAdapter(adapter) call in a Runnable and call it via a Handler from your Thread....

but don't!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM