0

i wrote an AsynkTask witch connect to a DB and then put the result in the UI. this works perfectly, but then i wanna include a Dialog or something, other Layout witch show a progressBar and then update the UI.

well, now the layout changes, and at the end i have the "testGUI" layout, but it doesn't upgrade with the AsynkTask Data. Here's the code. Thank you so much.

class DatabaseAsync extends AsyncTask {

    @Override
    protected void onPreExecute(){
        setContentView(R.layout.loading);
    }
    /**
     * Se ejecuta en el hilo secundario, en segundo plano
     */
    @Override
    protected JSONArray doInBackground(Void... params) {
        JSONArray jArray = new JSONArray();
        jArray = DatabaseController
                .rellenarArray("SomeURL");
        return jArray;
    }

    /**
     * Una vez terminado el hilo secundario se ejecuta esto
     */
    @Override
    protected void onPostExecute(JSONArray jArrayFull) {
        setContentView(R.layout.testgui);
         getQuestion(jArrayFull,1);
    }

}
Santanor
  • 566
  • 1
  • 7
  • 20
  • You should not call setContentView multiple times, look here for further info: http://stackoverflow.com/questions/4018772/calling-setcontentview-multiple-times – marcinj Apr 15 '12 at 21:39
  • either `getQuestion(jArrayFull,1);` does not update any UI or `DatabaseController.rellenarArray("SomeURL");` does not return the data you expect. You need to check that. – zapl Apr 15 '12 at 21:40
  • zapl, if i DON'T call the second layout everything works perfectly, both methods works correctly ;) the problem is with the Layouts ;) – Santanor Apr 15 '12 at 21:43

2 Answers2

2

You can't use setContentView() twice. You will have to get a reference to each view and update it by changing their properties in onPostExecute().

Akhil
  • 13,888
  • 7
  • 35
  • 39
0

How does your UI appear after you have shown the progress bar?

If the logic is as follows:

  1. on UI press a btn or sth to start the DB AsyncTask
  2. start the dialog activity which includes a progress bar that you want to update frequently
  3. the DB task does it's work and updates the dialog activity with progress bar
  4. once the DB task is done, it informs the dialog acticity that it is done
  5. the dialog activity reaches 100% and/or tells the user that everything is ok and finished
  6. the user may press an "OK" btn on the dialog activity and returns to the UI activity where the user initialized the start of the DB task

...then you need to work with handlers to update the dialog activity.

And once the user presses "OK" in step 6, the UI activity resumes - so you have to make sure that whatever you want to show on the UI activity gets updated in the onResume of your UI activity

Any direct call from your asynctask to the UI should be avoided - use handlers to communicate fom your background worker task to the UI.

If you need more information about how to do that - just look for handler and progress dialog, there are couple of tutorials.

If you do not use handlers but call the UI activity methods directly from the backgroudn worker activity the UI may freeze and be even removed by the Android system in the worst case.

If you do not need to frequently update the progress bar then you may just call:

textView.setText(result); in your onPostExecute method 

or any other direct setting of a UI element

user387184
  • 10,953
  • 12
  • 77
  • 147