android - doInBackground总是返回false

时间:2016-05-19 20:08:56

标签: android android-asynctask

我试图在列表加载AsyncTask中的数据时显示ProgressDialog,但是' exito' inPostExecute永远不会成立,对话框永远不会被忽略。 我试图删除if (exito)但却取消了progressDialog,几秒后该列表就被收费了,这不是我想要的。 我想在加载时显示progressDialog,加载时,关闭progressDialog并更改片段。

我的错误在哪里?感谢

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();
            }
        }
    }

3 个答案:

答案 0 :(得分:2)

rellenaLista()是异步的。

由于它在不同的主题上运行,因此return completado;会在您到达onSuccess()之前执行,因此completado仍为false

您真的不需要AsyncTask

您可以执行以下操作:

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
        }
    });
}

答案 1 :(得分:0)

我认为方法compruebaConexion()总是假的,如果你可以添加这个方法的代码的问题。我可以承认这个想法。

答案 2 :(得分:0)

创建这样的类。并使用它检查互联网连接

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;
    }
}
  

用法:

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

}
else
{
        //no internet.
}