2011-02-24 4 views
1

내 사용자 정의 대화 상자에 구하는 데 오랜 시간이 걸리는 목록이 있습니다. 그래서 AsyncTask를 사용하여 onPrepareDialog에서 시작된 별도의 스레드에로드합니다. AsyncTask.execute를 호출하기 전에 ProgressDialog를 호출합니다. 문제는 대화 상자가 처음으로 호출 될 때 (onCreateDialog가 호출 될 때) ProgressDialog가 내 (빈) 대화 상자 뒤에 표시된다는 것입니다. hr에서 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); 
    } 
} 
+3

당신은 여기에 코드의 조각을 붙여주십시오 수 있습니다. –

답변

1

사용자 정의 대화 상자에서 onCreateDialog 내부에서 AsyncTask.execute를 호출 해보십시오.

+0

나는 onCreate 내에서 말하고 싶습니다. – njzk2

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); 

당신은 다음, 마지막으로 선언 할 필요가 없습니다 것 중 하나 :)

+0

progressDialog를 좀 더 '로컬'로 만드는 데 좋은 아이디어지만, 불행히도 동작을 변경하지는 않습니다. –

관련 문제