سؤال

I'm using following code to fill a custom ListPreference dialog. Since the fill procedure takes a lot of time i want to show a progress dialog during the fill procedure.

My problem is that filler.execute() does not block onPrepareDialogBuilder and functions goes till the end before values are filled causing an exception... Any idea?

@Override
protected void onPrepareDialogBuilder(Builder builder) {        
    // Load data
    if (this.getEntries()==null) {
        FillerTask filler = new FillerTask();
        filler.execute();
    }   
    Log.d(TAG, "Filler finished");
    super.onPrepareDialogBuilder(builder);
}

Here is Filltertask code, basically he looks for every activity with a MAIN Intent filling a list:

private class FillerTask extends AsyncTask<Void, Void, String[][]> {
    private ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        Log.d(TAG, "Dismiss dialog");
        dialog = ProgressDialog.show(MyListPreference.this.getContext(), "", "Doing stuff...", true);
    }

    @Override
    protected String[][] doInBackground(Void... params) {
        return fill();
    }

    public String[][] fill() {
    Log.d(TAG, "Fill started");

        CREATE LISTS...

        // Done
        Log.d(TAG, "Fill done");
        String[][] result = new String[][] {entryNames, entryValues};
        return result;
    }


    @Override
    protected void onPostExecute(String[][] result) {
        Log.d(TAG, "Post execute");
        MyListPreference.this.setEntries(result[0]);
        MyListPreference.this.setEntryValues(result[1]);
        dialog.dismiss();
    }
 }  
هل كانت مفيدة؟

المحلول 2

Found the solution, best way to do this is override the onClick method and let the AsyncTask postExecute call the "super()", so click is not passed until content is loaded and during load progress bar is correctly displayed.

نصائح أخرى

My problem is that filler.execute() does not block onPrepareDialogBuilder and functions goes till the end before values are filled causing an exception... Any idea?

That is the entire point behind an AsyncTask. The "Async" in AsyncTask means asynchronous.

Use your AsyncTask to get your data. Then, in onPostExecute(), display the dialog.

asyntask doesn't lock main thread, it just drops a message to message queue of main thread

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top