Jyosna
Jyosna

Reputation: 4446

android problem with progress dialog

In my app i m trying to fetch data from server and storing in database. When it is doing all these work i want at that time progressdialog should show, if successfully data fetches then dialog should close and some alertDialog should show for msg "successfully data fetched". and if any n/w problem there, then it should show different msg that "problem with n/w".

for that i am doing like below,

public void onClick(View arg0) {
                myProgressDialog = ProgressDialog.show(
                        getParent(), "Please wait...",
                        "Doing upgrade...", true);
                new Thread() {
                    public void run() {
                        try {

                        upgradeAll();//function where data fetched from server

                            sleep(5000);

                        } catch (Exception e) {
                        }
                        // Dismiss the Dialog

                        myProgressDialog.dismiss();


                        Toast.makeText(UpgradeAllTableData.this, "Due to some internal problem \n" +
                                "it couldnot update..", Toast.LENGTH_LONG).show();
                    }

                }.start();
            }

AsyncTask code,

private class UpgradeTask extends AsyncTask<Context, Void, Void> {
        private ProgressDialog Dialog = new ProgressDialog(UpgradeAllTableData.this);

        @Override
        protected void onPreExecute() {
            System.out.println("In onPreExecute ");
            Dialog.setTitle("Loading");
            Dialog.setMessage("Please wait for few seconds...");
            Dialog.show();
        }

        @Override
        protected Void doInBackground(Context... params) {
            System.out.println("In doInBackground ");

            upgradeAll();

            System.out.println("In doInBackground after fetching");
            return null;
        }

        @Override
          protected void onPostExecute(Void unused) {

            System.out.println("In onPostExecute ");
            Dialog.dismiss();

            Toast.makeText(UpgradeAllTableData.this, "Problem with internet" ,  Toast.LENGTH_SHORT).show();
            alertboxNeutral("Warning", "Problem with Internet Connection", "Okay","Try again");

        }
    }

Problem is Toast is not showing. why?

My question is which condition and where to give so that if any problem with n/w then it show some msg and success then show another msg.

Where i should write code for that? Give me some suggestion.

Thank you

Upvotes: 0

Views: 556

Answers (2)

ngesh
ngesh

Reputation: 13501

What you are doing is totally wrong. UI thread handles all UI changes, but here you are creating ProgressDialog in UI thread and dismissing it in some Other Thread.. A solution is make use of AsyncTask http://developer.android.com/reference/android/os/AsyncTask.html

Upvotes: 1

K-ballo
K-ballo

Reputation: 81349

Not exactly an answer to your particular question, but have you considered AsyncTask? It's pretty much designed to handle situations like yours, where a task is performed async while showing some progress (and eventual result) on the UI thread. Alternativelly, you could broadcast an intent and have your activity catch it to show the toast, since your Toast should be show from your UI thread as well.

update: AsyncTask reference - http://developer.android.com/reference/android/os/AsyncTask.html

Upvotes: 1

Related Questions