Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You should do this with the help of <a href="http://developer.android.com/intl/de/reference/android/os/AsyncTask.html" rel="nofollow">AsyncTask</a> (an intelligent backround thread) and <a href="http://developer.android.com/intl/de/reference/android/app/ProgressDialog.html" rel="nofollow">ProgressDialog</a></p> <p>AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.</p> <p>An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called begin, doInBackground, processProgress and end.</p> <p><strong>The 4 steps</strong></p> <p>When an asynchronous task is executed, the task goes through 4 steps:</p> <p><code>onPreExecute()</code>, invoked on the UI thread immediately after the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.</p> <p><code>doInBackground(Params...)</code>, invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step. This step can also use publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.</p> <p><code>onProgressUpdate(Progress...)</code>, invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.</p> <p><code>onPostExecute(Result)</code>, invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter. Threading rules</p> <p><strong>There are a few threading rules that must be followed for this class to work properly:</strong></p> <p>The task instance must be created on the UI thread. execute(Params...) must be invoked on the UI thread. Do not call onPreExecute(), onPostExecute(Result), doInBackground(Params...), onProgressUpdate(Progress...) manually. The task can be executed only once (an exception will be thrown if a second execution is attempted.)</p> <p><strong>Example code</strong><br> What the adapter does in this example is not important, more important to understand that you need to use AsyncTask to display a dialog for the progress.</p> <pre><code>private class PrepareAdapter1 extends AsyncTask&lt;Void,Void,ContactsListCursorAdapter &gt; { ProgressDialog dialog; @Override protected void onPreExecute() { dialog = new ProgressDialog(viewContacts.this); dialog.setMessage(getString(R.string.please_wait_while_loading)); dialog.setIndeterminate(true); dialog.setCancelable(false); dialog.show(); } /* (non-Javadoc) * @see android.os.AsyncTask#doInBackground(Params[]) */ @Override protected ContactsListCursorAdapter doInBackground(Void... params) { cur1 = objItem.getContacts(); startManagingCursor(cur1); adapter1 = new ContactsListCursorAdapter (viewContacts.this, R.layout.contact_for_listitem, cur1, new String[] {}, new int[] {}); return adapter1; } protected void onPostExecute(ContactsListCursorAdapter result) { list.setAdapter(result); dialog.dismiss(); } } </code></pre>
 

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