1

カスタム ダイアログに、取得に時間がかかるリストがあります。そのため、AsyncTask を使用して onPrepareDialog で起動された別のスレッドにロードします。AsyncTask.execute を呼び出す前に、ProgressDialog を呼び出します。問題は、ダイアログが初めて呼び出されたとき (onCreateDialog が呼び出されたとき)、ProgressDialog が (空の) ダイアログの背後に表示されることです。時間の前に 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);
    }
}
4

2 に答える 2

1

カスタム ダイアログの onCreateDialog 内で AsyncTask.execute を呼び出してみてください。

于 2011-02-24T08:47:33.163 に答える
1

おそらく、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);

その場合、それを最終として宣言する必要はありません:)

于 2011-02-24T18:00:49.147 に答える