How to implement a ProgressDialog correctly so that time-consuming methods are called in background?
<p>I use an BaseAdapter to display a list of objects. This objects are fetched from a server. The getView() method of the BaseAdapter is the following:</p>

<pre><code>/* (non-Javadoc)
         * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LinearLayout content = (LinearLayout) View.inflate(lexs, R.layout.favorite_item, null);

            LinearLayout paragraphView = new LinearLayout(lexs); 
            paragraphView.setBackgroundColor(Color.RED);
            paragraphView.setGravity(Gravity.CENTER_VERTICAL);
            paragraphView.setOrientation(LinearLayout.HORIZONTAL);

            ImageView img = new ImageView(lexs);
            img.setImageResource(R.drawable.down);
            paragraphView.addView(img);
            img.setPadding(0, 0, 5, 0);
            img.setOnClickListener(new WorkspaceOnClickListener(position));

            TextView text = new TextView(lexs);
            text.setTextColor(Color.WHITE);
            text.setText(favorites.get(position).getParentChapter().getBook().getName() + ": §" + favorites.get(position).getName());
            text.setOnClickListener(new ParagraphOnClickListener(position));

            DisplayMetrics metrics = new DisplayMetrics();
            lexs.getWindowManager().getDefaultDisplay().getMetrics(metrics);
            int maxWidth = metrics.widthPixels - 100;

            text.setMaxWidth(maxWidth);
            text.setMinWidth(maxWidth);
            paragraphView.addView(text);

            ImageView imgClose = new ImageView(lexs);
            imgClose.setMinimumHeight(30);
            imgClose.setMinimumWidth(30);
            imgClose.setImageResource(R.drawable.close);
            imgClose.setPadding(5, 0, 0, 0);
            paragraphView.addView(imgClose);
            imgClose.setOnClickListener(new CloseListener(position));

            content.addView(paragraphView);
            content.setPadding(0, 0, 0, 10);

            if (favorites.get(position).isExpanded()) {
                LinearLayout textLayer = new LinearLayout(lexs);
                textLayer.setBackgroundColor(Color.rgb(214, 214, 214));

                LinearLayout left = new LinearLayout(lexs);
                left.setOrientation(LinearLayout.VERTICAL);

                ImageView moveUp = new ImageView(lexs);
                moveUp.setImageResource(R.drawable.move_up);
                moveUp.setOnClickListener(new MoveListener(UP, position));
                moveUp.setPadding(0, 0, 0, 10);
                left.addView(moveUp);

                ImageView moveDown = new ImageView(lexs);
                moveDown.setImageResource(R.drawable.move_down);
                moveDown.setOnClickListener(new MoveListener(DOWN, position));
                left.addView(moveDown);

                TextView paragraphText = new TextView(lexs);
                paragraphText.setText(favorites.get(position).getText());
                paragraphText.setTextColor(Color.BLACK);

                LinearLayout right = new LinearLayout(lexs);
                right.setOrientation(LinearLayout.HORIZONTAL);
                right.addView(paragraphText);

                textLayer.addView(left);
                textLayer.addView(right);
                content.addView(textLayer);

                img.setImageResource(R.drawable.up);
            }

            return content;
        }

    }
</code></pre>

<p>So I'd like that this whole method is called in background and during the method is executed, a ProgresDialog is shown. I defined the ProgressDialog the following way:</p>

<pre><code>public class LoadingInformation {

    private static ProgressDialog progressDialog;

    public static void showProgressInformation(Context view) {

        progressDialog = ProgressDialog.show(view, "Please wait...", "Doing Extreme Calculations...", true);
        System.out.println("Start Loading Screen");

    }

    public static void stopShowingProgressInformation() {

        Handler handler=new Handler();
        handler.post(new Runnable(){public void run(){progressDialog.dismiss();}});
        System.out.println("Stop Loading Screen");

    }

}
</code></pre>

<p>But the problem is, i don't know how to implement the calls correctly. I tried to replace all lines:</p>

<pre><code>adapter.notifyDataSetChanged();
</code></pre>

<p>With the following code:</p>

<pre><code>public void updateFavoriteList() {

        LoadingInformation.showProgressInformation(lexs);

        new Thread() {

            public void run() {

                lexs.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        adapter.notifyDataSetChanged();
                        LoadingInformation.stopShowingProgressInformation();
                    }

                });



            }

            }.start();


    }
</code></pre>

<p>But unfortunately this doesn't work as expected. Has anybody a hint how to do it betteR?</p>
