cnbandicoot
cnbandicoot

Reputation: 370

android - doInBackground return always false

I'm trying to shows a ProgressDialog while a list is loading data in an AsyncTask, but 'exito' in onPostExecute is never true, and the dialog never dismiss. I tried to delete the if (exito) but the progressDialog dismiss and the list is charged a few seconds later, and it isn't I want. I want that progressDialog shows while is loading, and when is loaded, dismiss the progressDialog and change fragment.

Where is my mistake? Thanks

private class ATCargarProductos extends  AsyncTask<Void, Integer, Boolean>{

        boolean terminado = false;
        Bundle bdl;
        FragmentTransaction transaction;
        ProgressDialog progressDialog;
        ArrayList<ItemDetails> results = new ArrayList<ItemDetails>();

        public ATCargarProductos(FragmentTransaction transaction){
            this.transaction = transaction;
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            if (compruebaConexion()) {

                rellenaLista(new CallBack() {
                    @Override
                    public void onSuccess(final ArrayList<Comida> listaComidas) {
                        for (int i = 0; i < listaComidas.size(); i++) {
                            ItemDetails item_details = new ItemDetails(listaComidas.get(i));
                            if (item_details.getTipo().equals("B")) {

                                results.add(item_details);
                            }
                        }

                        Fragment fragmentProductos = new FragmentProductos();
                        bdl = new Bundle(2);
                        bdl.putInt("tipoProducto", 1);
                        bdl.putParcelableArrayList("resultados", results);
                        fragmentProductos.setArguments(bdl);

                        completado = true;

                    }

                    @Override
                    public void onFail(String msg) {

                    }
                });

                return completado;

            } else {
                return false;
            }
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = new ProgressDialog(getActivity(), R.style.AppTheme_Dark_Dialog);
            progressDialog.setIndeterminate(true);
            progressDialog.setMessage("Cargando lista...");
            progressDialog.show();
        }

        @Override
        protected void onPostExecute(Boolean exito) {
            super.onPostExecute(exito);
            if (exito) {
                progressDialog.dismiss();
                transaction.commit();
            }
        }
    }

Upvotes: 0

Views: 158

Answers (3)

Hayrullah Cansu
Hayrullah Cansu

Reputation: 262

Create a class like that. And check your internet connection with it.

public class EZInternetConnection {
    public static boolean isNetworkConnected(Context context)
    {
    ConnectivityManager cm =
            (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        boolean flag = cm.getActiveNetworkInfo() != null &&
            cm.getActiveNetworkInfo().isConnectedOrConnecting();
        return flag;
    }
}

Usage:

if(EZInternetConnection.isNetworkConnected( context ))   
{
      //internet connection is ok.
      //other codes.

}
else
{
        //no internet.
}

Upvotes: 0

Benito Bertoli
Benito Bertoli

Reputation: 25793

rellenaLista() is asynchronous.

Since it's running on a different thread, return completado; is executed before you reach onSuccess(), and therefore completado is still false.

You don't really need an AsyncTask.

You can do the following:

if (compruebaConexion()) {
    // show progress dialog here

    rellenaLista(new CallBack() {
        @Override
        public void onSuccess(final ArrayList<Comida> listaComidas) {
            // dismiss dialog
            // handle success
        }

        @Override
        public void onFail(String msg) {
            // dismiss dialog
            // handle failure
        }
    });
}

Upvotes: 2

vjsantojaca
vjsantojaca

Reputation: 325

I think that the method compruebaConexion()is always false, if you can add to the question the code of this method. I could admit this idea.

Upvotes: 0

Related Questions