Toast exhibition

Asked

Viewed 238 times

1

I’m creating an app Android where I have some Switch which each one is linked to an image. I use a API to bring them and make your shipment with Image Loader. When I have an error loading the images I use a Toast to inform the user. The problem is that when I select several Switch and there is an error in loading more than one image Toast remains on screen for a long time, as it enters more than once on Toast. So I was wondering how can I control the display of this message, showing the Toast a single time regardless of the number of images with loading error.

Example of the code I’m using to upload the image.

public void loading (){

final Radar finalRadar = radar;

ProductPresenter.getLastData(radar.getRadarId(), new ProductCallBack() {
    @Override
    public void onSuccess(final Product product) {

        final ImageLoader imageLoader = ImageLoader.getInstance();
        imageLoader.loadImage(product.getUrl(), new ImageLoadingListener() {

                @Override
                public void onLoadingStarted(String imageUri, View view) {

                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

                    loadImageError(finalRadar.getRadarId(), radar);

                    return;

                }

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

                    addRadarOnList(product, finalRadar, loadedImage);
                    reloadSubtitle(radar);
                }

                @Override
                public void onLoadingCancelled(String imageUri, View view) {

                    loadImageError(finalRadar.getRadarId(),radar);

                }
        });
    }

    @Override
    public void onFail() {

        loadImageError(finalRadar.getRadarId(),radar);
    }

});
}



    private void loadImageError(Object componentTag, Radar radar) {

    StyleableToast.makeText(context, "Erro ao baixar imagem!", R.style.mytoast).show();

}
  • You could create an accountant, I think in this case it would work well

  • 1

    @Murillocomino, I have a counter on the switch, counting how many were selected, but if I do an if (counter != 1) Toast.makeText(context, "Error while downloading image!") show(); no loadImageError will not give in the same?

1 answer

0

One solution would be to take these erros and send to the log, That way you’d know which images went wrong. I believe this function loading() execute n times according to the number of images you have.

Edit.: as well remembered in the comments, the method loading() performs an asynchronous anonymous function within it, thus requesting a more complex check.

Example of an iteration in which it reads the images

public boolean isSuccess = true;
public int size = 0;
public void executaMetodoLeitura(List<Radar> lstRadar){
  size = lstRadar.size();
  for(int i = 0; i< lstRadar.size(); i++){
    this.radar = lstRadar[i]; 
    loading(i);
  }
}

With this call, it would be necessary to create a function that "picks up" these returned values asynchronously. She is also responsible for checking whether the last asynchronous call value was sent (it is she who fires the Toast)

public void consultaErro(boolean isSuccess, int index){
 if(this.isSuccess) this.isSuccess = isSucess; 

 if(index == this.size){ 
  disparaMensagem(this.isSucess);
 } 
}

The reading method would look like this:

public void loading (int index){

    final Radar finalRadar = radar;
    boolean isSuccess = true;

    ProductPresenter.getLastData(radar.getRadarId(), new ProductCallBack() {
        @Override
        public void onSuccess(final Product product) {

            final ImageLoader imageLoader = ImageLoader.getInstance();
            imageLoader.loadImage(product.getUrl(), new ImageLoadingListener() {

                @Override
                public void onLoadingStarted(String imageUri, View view) {

                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

                    consultaErro(false,index);
                    Log.i("Loading Image Failed", String.format("Falha ao carregar{0}!",radar.getRadarId()));
                    return;

                }

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    consultaErro(true,index);
                    addRadarOnList(product, finalRadar, loadedImage);
                    reloadSubtitle(radar);
                }

                @Override
                public void onLoadingCancelled(String imageUri, View view) {
                    consultaErro(false,index);
                    Log.i("Loading Image Failed", String.format("{0} : Leitura cancelada",radar.getRadarId()));
                }

                @Override
                public void onFail() {
                    consultaErro(false,index);
                    Log.i("Loading Image Failed", String.format("Ocorreu um erro ao carregar {0}",radar.getRadarId()));
                }
            });
        }
    });
}

And finally, the function that triggers the message:


public void disparaMensagem(boolean isSucess){

  if(!isSuccess){
     StyleableToast.makeText(context, "Erro ao baixar imagem!", 
     R.style.mytoast).show();
  }

}

There are other ways to do this, sometimes it would be interesting to add this incorrect data in an array for messages of type "3 of 5 images were not loaded", but I think this solves your problem.

  • Are you forgetting that the methods of Productcallback are called asynchronously. When the method loading() is called it returns (almost) immediately, before any method of Productcallback be executed.

  • Damn, that’s a good point. What could be implemented is to put these returns within a method (which is only called after reading), in this case, it would be what would enable the return to be the one actually found, it makes sense?

  • Yes, it will have to be so.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.