omer schleifer
omer schleifer

Reputation: 3935

show toast or progressbar on alertDialog item selected

I have an alertDialog with a few items. When an Item is selected, there are some Web services calls and only when they are done the dialog closes. I would like to let the user know he/she shold wait.

I've tried using progress bar and Toast. In the Toast version, if I just show the Toast, it does show - but only after the dialog was closed.

I also tried running the Toast on another thread, which caused the application to crash.

Here is some example of the code:

private void chooseFromClosestSamplePoints(final ArrayList<Place> places) {     
        if (null != places) {
            final String[] items = new String[places.size()];
            for (int i = 0; i < places.size(); i++) {
                Place place = places.get(i);
                String displayText = place.mfactoryName + "," + place.msamplePointName;
                items[i] = displayText;
            }


                    AlertDialog.Builder builder = new AlertDialog.Builder(
                            Main.this);
                    builder.setTitle(R.string.select_sample_point);
                    builder.setItems(items,
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {    
                                    Place place = places.get(which);                                    
                                    Toast toast = Toast.makeText(getApplicationContext(), "אנא המתן", Toast.LENGTH_LONG);
                                    toast.show();
                                    boolean res = selectPlace(place); //this is the long running WS call
                                    if(!res)
                                    {
                                      mStartButton.setEnabled(false);
                                      showAlert("שגיאה","אנא הגדר מחדש עיר מפעל ונקודת דיגום", false);
                                    }
                                }
                            });
                    builder.show();
                }           
    }

Edit: Inside the SelectPlace method , there is some code run on the UI thread itself like so:

private boolean selectPlace(Place place){
GetFactories factoryMethod = new GetFactories(mApp, selectedCity);
        factoryMethod.call(); //This calls the WS and awaits results

        final ArrayList<Factory> factories =factoryMethod.getFactories();
        Factory selectedFactory = null;
        for(int i = 0; i < factories.size(); i++)
        {
///Some logic
}

//Some more logic...
}

Would appreciate any help. thanks,

Omer

Upvotes: 0

Views: 917

Answers (1)

Voicu
Voicu

Reputation: 17850

You might want to move your web service call code into an IntentService class, so the UI thread is not blocked while waiting for the web service to get a response back.

Looking at your code, factoryMethod.call(); is probably blocking the UI thread.

Upvotes: 1

Related Questions