views:

44

answers:

4

Hi i am dispalying some data by using sqlite .when i click on one button data come from database. It takes some time. At that time the screen is black .At that time I want to display the rotating spinner before the data dispaly.For this purpose give me some suggestions.Thanks in advance

A: 

You should not execute long running tasks in UI thread as this blocks the UI redraw and makes app look unresponsive.

Use AsyncTask to execute long running tasks in background, while still updating the screen.

Peter Knego
A: 

First i would like to suggest to have a look at AsyncTask page, so that you will come to know about the AsyncTask exactly.

Now, Implement AsyncTask as given below:

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        new performBackgroundTask().execute();
  }

  private class performBackgroundTask extends AsyncTask <Void, Void, Void>  
  {
           private ProgressDialog Dialog = new ProgressDialog(main.this);

           protected void onPreExecute()
           {
               Dialog.setMessage(getString("Please wait..."));
               Dialog.show();
           }

           protected void onPostExecute(Void unused)    
           {
               Dialog.dismiss();    
               // displaying all the fetched data
           }

           @Override
            protected Void doInBackground(Void... params) 
            { 
                // implement long-running task here i.e. select query/fetch data from table
                // fetch data from SQLite table/database
                return null;   
            }
  }

Enjoy !!!

PM - Paresh Mayani
A: 

You can look at the standard music picker as one example of how to do this:

http://android.git.kernel.org/?p=platform/packages/apps/Music.git;a=blob;f=src/com/android/music/MusicPicker.java

In addition to the whole "queries must be done off the main UI thread," this shows an indeterminant progress while first loading its data, fading to the list once the data is available. The function to start the query is here:

http://android.git.kernel.org/?p=platform/packages/apps/Music.git;a=blob;f=src/com/android/music/MusicPicker.java;h=c41746ddf2cbeb80e111d72139b42e8f01329c30;hb=HEAD#l581

And to do the switch is here:

http://android.git.kernel.org/?p=platform/packages/apps/Music.git;a=blob;f=src/com/android/music/MusicPicker.java;h=c41746ddf2cbeb80e111d72139b42e8f01329c30;hb=HEAD#l569

The layout has the list view put in a frame layout with another container holding the progress indicator and label. The visibility of these is changed to control whether the list or progress indicator are shown:

http://android.git.kernel.org/?p=platform/packages/apps/Music.git;a=blob;f=res/layout/music_picker.xml

hackbod
A: 

Android provides a ProgressDialog for accomplishing what you want.

takyon