tags:

views:

148

answers:

2

Hello guys. I have a little problem, I hope U can help me;)

Trouble is, that ProgressDialog show only after loading run(), but I need to show it on start and showing it while loading some data. I put: "dialog = ProgressDialog.show(CategoriesListActivity.this,"Working...","Loading data", true);" in method run(), but the same. I print in Log.i() some info (int i++) and put title of ProgressDialog. Method work correctly, but don't show ProgressDialog. I have read some info that some thread block another thread (my created), that's why doesn't show progressDialog, but can't do anything. Thx.

    public void run() {

        /** getting there long execution **/
        handler.sendEmptyMessage(0);

    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            // stop and hide dialog
            dialog.dismiss();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_

        dialog = ProgressDialog.show(CategoriesListActivity.this, "Working...",
                "Loading data", true);

        // start new thread where get long time execution
        Thread thread = new Thread(this);
        thread.start();

        //wait while data is loading, 'cause I need use variable from calculation
                // in "EfficientAdapter" later
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        ListView l1 = (ListView) findViewById(R.id.list);
        l1.setAdapter(new EfficientAdapter(this));
    }
A: 

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

When the AsyncTask starts we raise 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<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();
    }
}
Pentium10
A: 

Thx, but I still have a problem((( How can I imitate .join () method like with thread for waiting while "PrepareAdapter1" is working? I tried task.getStatus().equals(AsyncTask.Status.FINISHED) but nothing. I debugged, and see, that AsyncTask.Status is always RUNNING... How can I send from onPostExecute() some notify to resume or something like that. In Your example, you setAdapter() in method onPostExecute(), but I can't do it, I need do it in method onCreate() after loading all data from "PrepareAdapter1" in method doInBackground.

    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Task task = new Task();
            task.execute();


//there I need stop that method like with working Thread .join() and resume it after onPostExecute()

        if (task.getStatus().equals(AsyncTask.Status.FINISHED)) {

            l1 = (ListView) findViewById(R.id.list);

            l1.setAdapter(new EfficientAdapter(this));
            }
        }

private class Task extends AsyncTask<Void, Void, Void> {

        protected void onPreExecute() {
            showDialog(DIALOG_TASKING);
        }

        protected Void doInBackground(Void... unused) {

            //some getting data)
            return null;
        }

        protected void onPostExecute(Void unused) {
            dismissDialog(DIALOG_TASKING);
//threre I need resume method onCreate()

        }

    }

P.S. data loads good (I saw while debugging) and progressDialog work fine, what I need))

Yaroslav Boichuk
I have done, I l1.setAdapter(new EfficientAdapter(this)); put to the onPostExecute and send there Context from main class, and it works great. Thx a lot)
Yaroslav Boichuk