-1

i'm sorry for the weird title but i didn't find other ways to explain that. In a Fragment i have a ProgressDialog that has to be shown till every instructions inside the Fragment are completed. The problem is that it disappear 5 seconds before the layout has to be changed. Here is my code:

final View view = inflater.inflate(R.layout.fragment_lista, container, false);
GridView gv = (GridView) view.findViewById(R.id.gridView);
Gson gson = new GsonBuilder().serializeNulls().create();

ProgressDialog progressDialog = ProgressDialog.show(getActivity(), "", "Loading...", true);

    new Thread(new Runnable() {
        @Override
        public void run()
        {
            String urlLista = null;
            try {
                urlLista = connection("https://www.something.com"); //personal method for this connection
            } catch (IOException e) {
                e.printStackTrace();
            }
            String listaTrimmed = urlLista.trim();
            ListaManga listaManga = gson.fromJson(listaTrimmed, ListaManga.class);

            List prov = new ArrayList();
            for (int i = 0; i < 15; i++) {
                prov.add(listaManga.getManga().get(i));
            } //ArrayList of 15 elements

            GridAdapter gridadapter = new GridAdapter(getActivity(), prov); //custom adapter
            });

            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run()
                {
                    progressDialog.dismiss(); //Here is when the ProgressDialog obviously disappear
                    gv.setAdapter(gridadapter);
                    gridadapter.notifyDataSetChanged();
                }
            });
        }
    }).start();

If i have to be honest i expected that with my code, after the dismiss() of the ProgressDialog, everything in this Fragment would be successfully loaded but it isn't. I have to wait 5 more seconds till everything is loaded and i can't understand why, hope i'll get some answers here. If there are some questions about my code, just ask, i had to cut a lot of parts of it and i couldn't explain this question better sorry. I tried also to put the dismiss() of the ProgressDialog inside a post delayed Handler, it worked, but i cancelled the animation of the ProgressDialog.

Giorgio Antonioli
  • 15,771
  • 10
  • 45
  • 70

1 Answers1

1

Have you tried calling the dismiss() method last?

gv.setAdapter(gridadapter);
gridadapter.notifyDataSetChanged();
progressDialog.dismiss();
Goran Horia Mihail
  • 3,536
  • 2
  • 29
  • 40
  • Yes i tried it too, but it doesn't change. The strange thing is that in debug mode set the adapter and "refresh" it don't require time. So i was asking to myself: Where the application "need time" after dismiss the dialog? – Giorgio Antonioli Dec 08 '14 at 20:36