ProgressDialog 显示在另一个对话框后面

发布于 2024-10-18 20:49:31 字数 3643 浏览 2 评论 0原文

我的自定义对话框有一个需要很长时间才能获取的列表。因此,我使用 AsyncTask 将其加载到 onPrepareDialog 中启动的单独线程中。在调用 AsyncTask.execute 之前,我调用 ProgressDialog。问题是,当第一次调用我的对话框(调用 onCreateDialog)时,ProgressDialog 显示在我的(空)对话框后面。如何在 hr 前面显示 ProgressDialog?当我的对话框被关闭并再次显示时,显示顺序是正确的。

public class MyActivity extends Activity {
FileSelectDlg fileSelectDlg = null;
LayoutInflater inflater;
ArrayList<String> fileList;


@Override
public void onCreate(Bundle savedInstanceState) {

    inflater = LayoutInflater.from(this);
}
@Override
protected Dialog onCreateDialog( int id ) 
{
    fileSelectDlg = new FileSelectDlg( this );
    return fileSelectDlg;
}
@Override
    protected synchronized void onPrepareDialog( int id, Dialog dialog ) {
    fileSelectDlg.update_dlg_view( -1 );
}

public class FileSelectDlg extends Dialog {
    private Context                 context;
    private BaseAdapter             adapter;

    public FileSelectDlg( Context context ) {
        super(context);
        this.context = context;

        View content = inflater.inflate( R.layout.file_select, null );
        setContentView( content );

        adapter = new BaseAdapter() {
            @Override
            public int getCount() {
                if( fileList != null )
                    return fileList.size();
                return 0;
            }
            @Override
            public Object getItem(int position) {
                return position;
            }
            @Override
            public long getItemId(int position) {
                return position;
            }
            @Override
            public View getView( int position, View convertView, ViewGroup parent ) {
                TextView textView;
                if (convertView == null) {
                    convertView = inflater.inflate(R.layout.file_select_item, null);
                    textView = (TextView) convertView.findViewById(R.id.item_text);
                    convertView.setTag(textView);
                } else {
                    textView = (TextView)convertView.getTag();
                }
                if( fileList != null ) {
                    textView.setText( fileList.get( position ) );
                }
                return convertView;
            }
        };

        ListView listView = (ListView)findViewById( R.id.list );
        listView.setAdapter(adapter);
        listView.setOnItemClickListener( new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int clicked, long id ) {
                update_dlg_view( clicked );
            }
        });
    }
    public void update_dlg_view( int clicked ) {
        int option = option_get_list;
        String item = null;
        if( clicked >= 0 ) {
            item = fileList.get( clicked );
            fileSelector.setDir(item);
        }

        final ProgressDialog progressDialog = new ProgressDialog( context );
        progressDialog.setMessage( "wait..." );
        progressDialog.setCancelable(false);
        progressDialog.show();

        new AsyncTask<Integer, Integer, Long>() {
            protected Long doInBackground( Integer... _option ) {
                long option = _option[0];
                fileList = fileSelector.getList();
                return option;
            }
            @Override
            protected void onPostExecute( Long option ) {
                progressDialog.dismiss();
                FileSelectDlg.this.setTitle( fileSelector.getCurrentPath() );
                adapter.notifyDataSetChanged();
            }
        }.execute(option);
    }
}

My custom dialog has a list which takes long time to obtain. So I load it in a separate thread launched in onPrepareDialog using AsyncTask. Prior to calling AsyncTask.execute I invoke ProgressDialog. The problem is that when my dialog is called for the first time (onCreateDialog is invoked), ProgressDialog is shown behind my (empty) dialog. How can I show ProgressDialog in hr front? When my dialog is dismissed and shown again, the display order is correct.

public class MyActivity extends Activity {
FileSelectDlg fileSelectDlg = null;
LayoutInflater inflater;
ArrayList<String> fileList;


@Override
public void onCreate(Bundle savedInstanceState) {

    inflater = LayoutInflater.from(this);
}
@Override
protected Dialog onCreateDialog( int id ) 
{
    fileSelectDlg = new FileSelectDlg( this );
    return fileSelectDlg;
}
@Override
    protected synchronized void onPrepareDialog( int id, Dialog dialog ) {
    fileSelectDlg.update_dlg_view( -1 );
}

public class FileSelectDlg extends Dialog {
    private Context                 context;
    private BaseAdapter             adapter;

    public FileSelectDlg( Context context ) {
        super(context);
        this.context = context;

        View content = inflater.inflate( R.layout.file_select, null );
        setContentView( content );

        adapter = new BaseAdapter() {
            @Override
            public int getCount() {
                if( fileList != null )
                    return fileList.size();
                return 0;
            }
            @Override
            public Object getItem(int position) {
                return position;
            }
            @Override
            public long getItemId(int position) {
                return position;
            }
            @Override
            public View getView( int position, View convertView, ViewGroup parent ) {
                TextView textView;
                if (convertView == null) {
                    convertView = inflater.inflate(R.layout.file_select_item, null);
                    textView = (TextView) convertView.findViewById(R.id.item_text);
                    convertView.setTag(textView);
                } else {
                    textView = (TextView)convertView.getTag();
                }
                if( fileList != null ) {
                    textView.setText( fileList.get( position ) );
                }
                return convertView;
            }
        };

        ListView listView = (ListView)findViewById( R.id.list );
        listView.setAdapter(adapter);
        listView.setOnItemClickListener( new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int clicked, long id ) {
                update_dlg_view( clicked );
            }
        });
    }
    public void update_dlg_view( int clicked ) {
        int option = option_get_list;
        String item = null;
        if( clicked >= 0 ) {
            item = fileList.get( clicked );
            fileSelector.setDir(item);
        }

        final ProgressDialog progressDialog = new ProgressDialog( context );
        progressDialog.setMessage( "wait..." );
        progressDialog.setCancelable(false);
        progressDialog.show();

        new AsyncTask<Integer, Integer, Long>() {
            protected Long doInBackground( Integer... _option ) {
                long option = _option[0];
                fileList = fileSelector.getList();
                return option;
            }
            @Override
            protected void onPostExecute( Long option ) {
                progressDialog.dismiss();
                FileSelectDlg.this.setTitle( fileSelector.getCurrentPath() );
                adapter.notifyDataSetChanged();
            }
        }.execute(option);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一杯敬自由 2024-10-25 20:49:31

尝试在 onCreateDialog 中调用 AsyncTask.execute 来进行自定义对话。

try calling the AsyncTask.execute inside onCreateDialog for Custom Dialogue.

感性 2024-10-25 20:49:31

也许尝试将 ProgressDialog 移动到 AsyncTask 中并使用 onPreExecute() 来创建和显示它。这将使 ProgressDialog 与其操作更加紧密地联系在一起,并可能提供显示顺序所需的延迟。

    new AsyncTask<Integer, Integer, Long>() {
        ProgressDialog progressDialog;

        @Override
        protected void onPreExecute() {
            progressDialog = new ProgressDialog( context );
            progressDialog.setMessage( "wait..." );
            progressDialog.setCancelable(false);
            progressDialog.show();
        }

        @Override
        protected Long doInBackground( Integer... _option ) {
            long option = _option[0];
            fileList = fileSelector.getList();
            return option;
        }

        @Override
        protected void onPostExecute( Long option ) {
            progressDialog.dismiss();
            FileSelectDlg.this.setTitle( fileSelector.getCurrentPath() );
            adapter.notifyDataSetChanged();
        }
    }.execute(option);

那么你也不必将其声明为最终版本:)

Perhaps try moving the ProgressDialog into the AsyncTask and using onPreExecute() to create and display it. This would keep the ProgressDialog more tied to its operation, and may provide the delay you need for display order.

    new AsyncTask<Integer, Integer, Long>() {
        ProgressDialog progressDialog;

        @Override
        protected void onPreExecute() {
            progressDialog = new ProgressDialog( context );
            progressDialog.setMessage( "wait..." );
            progressDialog.setCancelable(false);
            progressDialog.show();
        }

        @Override
        protected Long doInBackground( Integer... _option ) {
            long option = _option[0];
            fileList = fileSelector.getList();
            return option;
        }

        @Override
        protected void onPostExecute( Long option ) {
            progressDialog.dismiss();
            FileSelectDlg.this.setTitle( fileSelector.getCurrentPath() );
            adapter.notifyDataSetChanged();
        }
    }.execute(option);

You wouldn't have to declare it as final then, either :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文