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);
}
}
try calling the AsyncTask.execute inside onCreateDialog for Custom Dialogue.