ProgressDialog until you finish loading a view using Thread

1

Well, I have a view in which I post a RSS and I'd like it to come out as% ProgressDialog while loading.

Currently I have it put to last 3 seconds, but I want to know how to put it to last until it finishes loading the view:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ver_noticias);
        TextView fuente = (TextView) findViewById(R.id.texto);
        String url = getResources().getString(R.string.rssUrl);
        final ProgressDialog ringProgressDialog = 
ProgressDialog.show(verNoticias.this, "Por favor espere ...",   "Cargando 
Informacion ...", true);
        ringProgressDialog.setCancelable(true);
        new Thread(new Runnable() {
        View vista;
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (Exception e) {
                }
                ringProgressDialog.dismiss();
            }
        }).start();
        fuente.setText("FUENTE: "+ url);
       CargarXmlTask tarea = new CargarXmlTask();
        tarea.execute(url);
    }
private class CargarXmlTask extends AsyncTask<String,Integer,Boolean> {
        protected Boolean doInBackground(String... params) {
            RssParserSax parser = new RssParserSax(params[0]);
// RssParserDom parser = new RssParserDom(params[0]);
            noticias = parser.parse();
            return true;
        }

        protected void onPostExecute(Boolean result) {
            System.out.println(noticias.toString());
            TextView txtResultado = (TextView) 
findViewById(R.id.textoNoticias);
//txtResultado.setText(noticias.toString());
            Noticia n;
            for(int i=0; i<noticias.size(); i++) {
                n = noticias.get(i);
                txtResultado.setText(
                        txtResultado.getText().toString() +
                                System.getProperty("line.separator") + 
n.toString() + System.getProperty("line.separator"));
            }

        }
    }

The solution that has served me has been the following:

   public class verNoticias extends AppCompatActivity {
   **boolean bandera=true;**
    List<Noticia> noticias;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ver_noticias);
    TextView fuente = (TextView) findViewById(R.id.texto);
    String url = getResources().getString(R.string.rssUrl);
    final ProgressDialog ringProgressDialog = ProgressDialog.show(verNoticias.this, "Por favor espere ...", "Cargando Informacion ...", true);
    ringProgressDialog.setCancelable(true);
    new Thread(new Runnable() {
        View vista;
        @Override
        public void run() {
           **while (bandera) {**
                try {
                    Thread.sleep(100);
                } catch (Exception e) {
                }
            **}**
            ringProgressDialog.dismiss();
        }
    }).start();
    fuente.setText("FUENTE: "+ url);
    CargarXmlTask tarea = new CargarXmlTask();
    tarea.execute(url);

}

//Tarea Asíncrona para cargar un XML en segundo plano

private class CargarXmlTask extends AsyncTask<String,Integer,Boolean> {
    protected Boolean doInBackground(String... params) {
        RssParserSax parser = new RssParserSax(params[0]);
// RssParserDom parser = new RssParserDom(params[0]);
        noticias = parser.parse();
        bandera=false;
        **return bandera;**
    }

    protected void onPostExecute(Boolean result) {
        System.out.println(noticias.toString());
        TextView txtResultado = (TextView) findViewById(R.id.textoNoticias);
//txtResultado.setText(noticias.toString());
        Noticia n;
        for(int i=0; i<noticias.size(); i++) {
            n = noticias.get(i);
            txtResultado.setText(
                    txtResultado.getText().toString() +
                            System.getProperty("line.separator") + n.toString() + System.getProperty("line.separator"));
        }
    }
}

}

    
asked by Jose Carlos 07.06.2017 в 11:24
source

4 answers

1

If what you need is to maintain your current structure, you can use a global flag like this:

boolean bandera = true;

then you add this in your Thread:

while(bandera) {
   try {
     Thread.sleep(100);
   } catch (Exception e) {}
}

Then in the part of your task where you finish loading the data, you add:

 bandera = false;

And with that it's enough ... but the simplest way and I think it's the best way to do it is to remove that Thread and do it with an Asynktask (assuming that is LoadXmlTask) from the onPostExcecute part. Check this: link

You can also use a Semaphore. Check here: link

    
answered by 07.06.2017 / 23:55
source
1

The ideal way to display ProgressDialog to be displayed while the RSS is being downloaded and stop showing when the RSS load is finished is to use ProgressDialog within AsyncTask . For this you will have to start the ProgressDialog in onPreExecute() and end the ProgressDialog in onPostExecute() , leaving your AsyncTask in the following way:

private class CargarXmlTask extends AsyncTask<String,Integer,Boolean> {

    private ProgressDialog pDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        pDialog = new ProgressDialog(mContext);
        pDialog.setMessage("Obteniendo datos");
        pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pDialog.show();
    }

    protected Boolean doInBackground(String... params) {
        RssParserSax parser = new RssParserSax(params[0]);
// RssParserDom parser = new RssParserDom(params[0]);
        noticias = parser.parse();
        return true;
    }

    protected void onPostExecute(Boolean result) {

        try{
            if (pDialog != null && pDialog.isShowing()){
                pDialog.dismiss();
            }
        }catch(Exception e){
            e.printStackTrace();
        }

        System.out.println(noticias.toString());
        TextView txtResultado = (TextView) 
findViewById(R.id.textoNoticias);
//txtResultado.setText(noticias.toString());
        Noticia n;
        for(int i=0; i<noticias.size(); i++) {
             n = noticias.get(i);
             txtResultado.setText(
                        txtResultado.getText().toString() +
                                System.getProperty("line.separator") + 
n.toString() + System.getProperty("line.separator"));
        }
    }
}

On the other hand, you must remove the ProgressDialog that you currently have.

    
answered by 08.06.2017 в 09:59
0

I think like @Joacer, although I think this is not necessary:

runOnUiThread(new Runnable() {
        public void run() {
            try{
                if (pDialog != null && pDialog.isShowing()){
                    pDialog.dismiss();
                    pDialog = null;
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    });

In the class from which you execute the Asynktask call, create a global Context variable and pass it to the ProgressDialog, in the postExecute you only have to do pDialog.dismiss (), I'll put it on top:

public class ClasePrincipal extends ActionBarActivity{
    Context cont = this;
    ProgressDailog pDialog;
    //Resto de cosas

    private class CargarXmlTask extends AsyncTask<String,Integer,Boolean> {
        @Override
        protected void onPreExecute() {

            pDialog = new ProgressDialog(cont);
            pDialog.setMessage("Espere..."); //Las opciones que quieras
            pDialog.setIndeterminate(true);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... urls) {
            //Operaciones necesarias
        }

        @Override
        protected void onPostExecute(String result) {
            //Operaciones necesarias
            pDialog.dismiss();
        }

    }
}
    
answered by 08.06.2017 в 11:44
-1

Remove part of new Thread(new Runnable ...).start()

You should close the progressDialog when you finish the task of loading news using this code:

ringProgressDialog.dismiss();

If the task is synchronous with putting it under tarea.execute(url); , although it gives me that it is asynchronous task (asynctask).

In such a case it is a little more complicated.

1- In your LoadXMLTask you must create an interface that will work as a callback:

 public interface Callbacks {

        void onPreExecute();
        void onProgressUpdate(int progress);
        void onPostExecute(int result);
        void onCancelled();
    }
    private Callbacks mListener;

2- Your activity should implement this callback, adding the methods 4 methods you put in.

 public class MainActivity extends AppCompatActivity implements
        CargarXMLTask.Callbacks

3- In your asynchronous task, have the onPostExecute method modified so that the callback calls the activity method:

 @Override
 protected void onPostExecute(Integer result) {
        if (mListener != null) {
            mListener.onPostExecute(result);
        }
 }

4- And already in your onPostExecute method of your activity you do the

 @Override
    public void onPostExecute(int result) {
        ringProgressDialog.dismiss();
    }

No one said it would be nice.

    
answered by 07.06.2017 в 14:49