0

im developing a app for showing data which is got from a web service to a list view.actually according to my application: when user clicks an item from a list view it should be asked some message with a dialog box. when user press "yes" there is an web service call process happening. so.. i want to show a progress dialog until the web service call process done(the method which is use to call web service returns a string message.i want to show that message after this progress dialog dismissed).

i am writing these code inside the getview method of my adapter class.

this is my code

    public View getView(final int position, View convertView, final ViewGroup parent) {
    // TODO Auto-generated method stub

    View vi = convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.dialog_row,null);


        //final TextView firstname = (TextView) vi.findViewById(R.id.fname);
        //final TextView lastname = (TextView) vi.findViewById(R.id.lname);
        final TextView startTime = (TextView) vi.findViewById(R.id.stime2);
        final TextView endTime = (TextView) vi.findViewById(R.id.etime2);
        final TextView date = (TextView) vi.findViewById(R.id.blank2);
        final TextView uid = (TextView) vi.findViewById(R.id.lname2);
        final TextView appid = (TextView) vi.findViewById(R.id.hidenappoinment);
        final ImageView img = (ImageView) vi.findViewById(R.id.list_image2);


        HashMap<String, String> song = new HashMap<String, String>();
        song =data.get(position);

        //firstname.setText(song.get(MainActivity.TAG_PROP_FNAME));
        //lastname.setText(song.get(MainActivity.TAG_PROP_LNAME));
        startTime.setText(song.get(MainActivity.TAG_STIME));
        endTime.setText(song.get(MainActivity.TAG_ETIME));
        date.setText(song.get(MainActivity.TAG_DATE));
        uid.setText(song.get(ShortList.TAG_UID));
        appid.setText(song.get(ShortList.TAG_APOINMENTID));



        //imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), img);





        vi.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                //String getFname = firstname.getText().toString();
                Toast.makeText(parent.getContext(), "view clicked: " , Toast.LENGTH_SHORT).show();

                //get the id of the view
                //check the id of the request
                //call the web service acording to the id

                AlertDialog.Builder alertbuild = new AlertDialog.Builder(activity);

                alertbuild.setMessage("Everything else will be Considerd as Rejected! Are You Sure ?");
                alertbuild.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(final DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        //Toast.makeText(mContext, "Yes ", Toast.LENGTH_LONG).show();

                        final String getStime = startTime.getText().toString();
                        final String getEtime = endTime.getText().toString();
                        final String getDate = date.getText().toString();
                        final String getUid = uid.getText().toString();
                        final String getapid = appid.getText().toString();


                        final ProgressDialog ringProgressDialog = ProgressDialog.show(activity, "Please wait ...", "Request is in Progress ...", true);
                                    ringProgressDialog.setCancelable(false);

                                    new Thread(new Runnable() {
                                        @Override
                                    public void run() {
                                            try {
                                                // Here you should write your time consuming task...
                                                // Let the progress ring for 10 seconds...


                                            //  ShortList sendandget = new ShortList();

                                            //    String resp = sendandget.sendDataAndGetResponce(getDate, getStime, getEtime,getUid,getapid);

                                            //    if(resp.equalsIgnoreCase("true")){


                                            //    }
                                                Thread.sleep(10000);


                                            } catch (Exception e) {

                                                e.printStackTrace();
                                        }
                                            ringProgressDialog.dismiss();
                                            Toast.makeText(mContext, "Your Request Accepted", Toast.LENGTH_LONG).show();
                                            dialog.dismiss();
                                        }
                                    }).start();




                    //  ArrayList<HashMap<String, String>> rfejected = getRejected.getRejectedList(getDate, getStime, getEtime,getUid,getapid);
                        //ArrayList<HashMap<String, String>> accept = getRejected.getAccept(getDate, getStime, getEtime,getUid,getapid);

                    //  sendaceptedAndReject(accept,getUid,rfejected,getapid);



                    }
                })

                .setNegativeButton("NO", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        Toast.makeText(mContext, "No ", Toast.LENGTH_LONG).show();
                    }
                });

                alertbuild.show();


            }
        });

        return vi;



}

i have tried to put a toast message to know weather it is working or not.the progressdialog is showing, but after the time which i gave to wait its just dismissed without showing my toast message.

please someone help me

i have tried using assync task

this is my coding..i didnt do any complex things.. just want to C it working or not.. but OnPreExecute() it is not shown the progressDialog :(

    public class ShowResponce extends AsyncTask<String, Void, String>{

    private ProgressDialog pDialog;

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        pDialog = new ProgressDialog(activity);
        pDialog.setMessage("Getting Data ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();


    }

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        //ShortList sendandget = new ShortList();
        //String resp = sendandget.sendDataAndGetResponce(getDate, getStime, getEtime,getUid,getapid);
        String x="a";

        return x;
    }

    @Override
    protected void onPostExecute(String x) {
        // TODO Auto-generated method stub
         pDialog.dismiss();

         Toast.makeText(mContext, x, Toast.LENGTH_LONG).show();

    }





}
Gishantha Darshana
  • 163
  • 1
  • 6
  • 17

6 Answers6

0

You cannot show the Toast Messages in a Individual Thread as it is not linked to your UI Thread.

You have to Handler concept or runOnUiThread() to show the Toast Message after your work done.

Check below code for runOnUiThread() and Handler class, they may help you.

runOnUiThread(new Runnable() {
                 public void run() {

                     Toast.makeText(mContext,"your message",Toast.LENGTH_LONG).show();
                }
            });

The below is Alternative way using Handler.

Handler h = new Handler({
 public void handleMessage(Message m) {
                  Toast.makeText(mContext,"your message",Toast.LENGTH_LONG).show();
            }
});

Then call the below statement when you want to show message.

h.sendEmptyMessage(0);
TNR
  • 5,839
  • 3
  • 33
  • 62
  • I have added one more line of code to call the Handler. and thats it the code sample you required. You define Handler Object anywhere as innerclass in Activity and call the h.sendEmptyMessage(0) whenever you want to show message. or else user runOnUiTjread() where you want to show toast message. – TNR Dec 09 '13 at 09:41
0

You cannot do any work related to UI inside any Thread. For this, you have to use Hanlder or just use AsyncTask to call web-servicee and show your Toast in onPostExecute() of AsyncTask.

amit singh
  • 1,407
  • 2
  • 16
  • 25
  • i have done like that..but since im in a class which is not an activity, and extends by BaseAdapter it produces an error saying that 12-09 16:19:45.800: E/AndroidRuntime(14054): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application can you help me with this ? – Gishantha Darshana Dec 09 '13 at 10:56
  • In your BaseAdapter class, you must be passing the object of your activity, then do it like this- Toast.makeText(activityInstance, "Toast", Toast.LENGTH_LONG).show(); – amit singh Dec 09 '13 at 11:08
  • @Gishantha Darshana.....yes, that's ok. If "mContest" is the Activity instance, then you must be seeing the Toast ? – amit singh Dec 09 '13 at 11:32
0

You can use runOnUiThread() For example

this.runOnUiThread(show_toast);

and in show_toast

private Runnable show_toast = new Runnable()
{
    public void run()
    {
        Toast.makeText(Autoamtion.this, "My Toast message", Toast.LENGTH_SHORT)
                    .show();
    }
};

Just click ~Toast from a Non-UI thread

Community
  • 1
  • 1
MengMeng
  • 1,016
  • 6
  • 11
0

use RunOnUiThread to show toast from another thread

runOnUiThread(new Runnable() {
    public void run() {
        //Toast
    }
});
null pointer
  • 5,874
  • 4
  • 36
  • 66
0

Use handler class to display Toast message.

 dialog = ProgressDialog.show(mContext, "Your Application", "Processing...",
            true);

 final Handler handler = new Handler() {
public void handleMessage(Message msg) {
    dialog.dismiss();
           Toast.makeText(mContext, "Your Request Accepted", Toast.LENGTH_LONG).show();
                                        dialog.dismiss();
  }

  callService = new Thread() {
        public void run() {
            try {
                result = CallWebserice(address);
                handler.sendEmptyMessage(0);
            } catch (Exception e) {
            }
        }
    };
  callService.start();
Vimal Rughani
  • 234
  • 4
  • 14
0

follow http://developer.android.com/guide/components/processes-and-threads.html

do like this

public void onClick(View v) {
    new Thread(new Runnable() {
        public void run() {
            Bitmap b = loadImageFromNetwork("http://example.com/image.png");
            mImageView.setImageBitmap(b);
        }
    }).start();
}

and changed to

public void onClick(View v) {
    new Thread(new Runnable() {
        public void run() {
            final Bitmap bitmap = loadImageFromNetwork("http://example.com/image.png");
            mImageView.post(new Runnable() {
                public void run() {
                    mImageView.setImageBitmap(bitmap);
Toast.makeText(mContext,"your message",Toast.LENGTH_LONG).show();

                }
            });
        }
    }).start();

}

Jitesh Upadhyay
  • 5,244
  • 2
  • 25
  • 43