How to display a “Loading…” text while retrieving items for a ListView

后端 未结 3 940
无人共我
无人共我 2021-02-08 12:33

There are some others applications doing this, like Twitter, Facebook, or even native applications such as Android Market. When you want to display a list of items retrieved fro

3条回答
  •  抹茶落季
    2021-02-08 13:00

    That's done with the help of AsyncTask (an intelligent backround thread) and ProgressDialog

    When the AsyncTask starts we reaise a progressdialog with indeterminate state, once the task is finished we dismiss the dialog.

    Example code
    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.

    private class PrepareAdapter1 extends AsyncTask {
        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();
        }
    }
    

提交回复
热议问题