I need to load data from the database into an ArrayList , in the second thread, accompanying the unloading by DialogFragment'ом . What can this be done with?


I wanted to do it through IntentServise , but he is not friendly with the List'ами (as far as I understood). I tried to start the unloading in the second thread, having stopped the first one, but the ProgressDialog sleeping with the main thread. I even tried to do everything in the main thread, but ProgressDialog does not have time to start and hangs with the main thread ...

    2 answers 2

    This is done through AsyncTask , an approximate scheme is as follows:

    1. We create the class inheriting from AsyncTask
    2. In the class constructor, create a ProgressDialog
    3. In the onPreExecute() method, onPreExecute() show the progressDialog.show() dialog, onPreExecute() out other activities - preparing a database for reading and so on.
    4. In the doInBackground() method, doInBackground() organize data reading from the database in a loop and during the reading call this.publishProgress() , which will call onProgressUpdate() (see below)
    5. In the onProgressUpdate() method, increment the progressDialog.setProgress() counter
    6. In the onPostExecute() method, close the progressDialog.dismiss() counter
    • And AsyncTask will not create problems? He is unreliable in dealing with long operations. - user189127
    • Who said such a heresy? - Barmaley
    • Here - stackoverflow.com/questions/469382/… and in Google. - user189127
    • @ bukashka101 you either listen to the advice of old fighters or google further. The algorithm I described is not a theoretical speculation - it is taken from my own code, which normally works perfectly on an up-to-date database of 10 thousand entries - Barmaley
    • Today I will get and try. True to turn down over the rotation of the screen will have (well, that the startandroi'e is on this lesson). - user189127

    Try to do it through AsyncTask , it has onPreExecute and onPostExecute , which are executed respectively, before and after doInBackground on the UI stream (more precisely, on the stream that AsyncTask called execute() ). Show and hide your DialogFragment in these methods, and load from the base in doInBackground