0

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

Jyosna
  • 4,436
  • 13
  • 60
  • 93

2 Answers2

1

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

K-ballo
  • 80,396
  • 20
  • 159
  • 169
  • I did that with ASyncTask. But its not wotking properly. thats why i tried with thread. – Jyosna Sep 10 '11 at 06:44
  • You were probably using AsyncTask wrong, better post an answer about why your AsyncTask implementation is not working properly. Or you could always use the intent solution suggested, in order to give the UI thread the chance to display the Toast. But I strongly suggest you use AsyncTask. – K-ballo Sep 10 '11 at 06:50
  • will i give my asynctask code. just check where i m going wrong? – Jyosna Sep 10 '11 at 06:52
  • Sure, just point me to the question once you have created it. – K-ballo Sep 10 '11 at 06:53
  • You should be calling publishProgress from your doInBackground and onPostExecute functions, and then actually showing the toast in your onProgress function. – K-ballo Sep 10 '11 at 07:01
  • before upgradeAll() function or after? – Jyosna Sep 10 '11 at 07:03
  • Actually, on PreExecute and onPostExecute are called on the UI thread. Let me check your code again and edit my answer. – K-ballo Sep 10 '11 at 07:03
1

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

ngesh
  • 13,398
  • 4
  • 44
  • 60