ohadinho
ohadinho

Reputation: 7144

How to show ProgressDialog while loading UI items?

I have a listview and I'm loading dynamically items into it. The problem is when I'm activating that activity. It takes too long, so I have to show some progress dialog while loading the UI. I really don't know how to do it, since doInBackground (If using Async task) doesn't allow the developer to mess with the UI. What should I do ?

Here is my UI load code :

LeagueTableRow leagueTableRow_data[] = new LeagueTableRow[]
                    {
                        new LeagueTableRow(1,R.drawable.logo,"aaa",12,9,3,14),
                        new LeagueTableRow(3,R.drawable.logo,"aaa",12,9,3,14),
                        new LeagueTableRow(4,R.drawable.logo,"aaa",12,9,3,14),
                        new LeagueTableRow(5,R.drawable.logo,"aaa",12,9,3,14),
                            new LeagueTableRow(2,R.drawable.logo,"aaa",12,9,3,14)
                    };

                    LeagueTableRowAdapter adapter = new LeagueTableRowAdapter(context, 
                            R.layout.leaguetablecontent, leagueTableRow_data);


                    listViewLeagueTable = (ListView)findViewById(R.id.listViewLeagueTable);

                    View header = (View)getLayoutInflater().inflate(R.layout.leaguetableheader, null);
                    listViewLeagueTable.addHeaderView(header);

                    listViewLeagueTable.setAdapter(adapter);

Upvotes: 2

Views: 1641

Answers (3)

Korhan Ozturk
Korhan Ozturk

Reputation: 11308

That can be achieved with the help of AsyncTask (an intelligent backround thread) and ProgressDialog

A ProgressDialog with indeterminate state would be raised when the AsyncTask starts, and the dialog is would be dismissed once the task is finished .

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<Void,Void,ContactsListCursorAdapter > {
    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();
    }
}

Upvotes: 3

nandeesh
nandeesh

Reputation: 24820

If you set the emptyview using listView.setEmptyView . The view will show in the place of list until you do a setadapter on the list. So As soon as you do setAdapter the emptyview will disappear. You can pass Progressbar to the setEmptyview in your case to show the progressbar.

Edit:
Use Setadapter in OnPostExecute of the Asynctask. OnPostExecute, OnPreExecute, OnProgressUpdate, OnCancelled all run on UI thread. If you still insist on ProgressDialog, then create dialog in OnPreExecute and dismiss it in OnpostExecute and OnCancelled of the Asynctask

Upvotes: 2

Dev.Sinto
Dev.Sinto

Reputation: 6852

Please use AsyncTask if loading of list is taking too much time.Place a progress bar on the top of list view use that in AsyncTask.Hope this will help you

Upvotes: 0

Related Questions