2012-02-11 11 views
0

単語のタイトルが謝罪されました。私は(私のonCreate()メソッド内)にしようとしています:ProgressDialog +ポーズコードの実行中に背景が読み込まれる

をいくつか-initialize

-display私はその後、進捗ダイアログ-clear私のバックエンド

からいくつかのデータをロードしながら進行状況ダイアログコードを続ける

典型的な進捗ダイアログソリューションはasynctaskであることを理解していますが、私はこれを試してみました(下記参照)。しかし、これは私が望むようにコード実行をロックしません。 lwpd.execute()の後に来るコードは、既に起こっている読み込みに依存しています。私はこれを過度に複製していますか?私がしたいことをする正しい方法は何ですか?参考のため

、私Asynctask実装:

public class LoadWithProgressDialog extends AsyncTask<Void, Void, Boolean>{ 
    private ProgressDialog pd; //the progress dialog 
    private String title; //the title of the progress dialog 
    private String message; //the body of the progress dialog 
    private Runnable task; //contains the code we want to run in the background 
    private Context c; 


    public LoadWithProgressDialog(Context context,String t, String m,Runnable r){ 
     super(); 
     c = context; 
     task = r; 
     title = t; 
     message = m; 
    } 

    @Override 
    protected void onPreExecute(){ 
     pd = ProgressDialog.show(c,title, message, false, false); 
    } 

    @Override 
    protected Boolean doInBackground(Void... params) { 
     task.run(); 
     return true; 
    } 
    @Override 
    protected void onPostExecute(Boolean result) { 
      pd.dismiss(); 
    } 





} 

super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 

      //initialize variables 


      LoadWithProgressDialog lwpd = new LoadWithProgressDialog(this,"Loading","Loading Truck Data", new Runnable() { 
       public void run(){ 
        //code that loads things 
       } 
      }); 
      lwpd.execute(); 

//continue on with my code 

} 

答えて

0

あなたは非同期タスクに進捗ダイアログを維持する必要があります。

データがロードされた後に実行するコードは、onPostExecuteメソッドに入れることができます。

public LoadWithProgressDialog(Context context,String t, String m,Runnable r){ 
    super(); 
    c = context; 
    task = r; 
    title = t; 
    message = m; 
} 

@Override 
protected void onPreExecute(){ 
    pd = ProgressDialog.show(c,title, message, false, false); 
} 

@Override 
protected Boolean doInBackground(Void... params) { 
    task.run(); 
    return true; 
} 
@Override 
protected void onPostExecute(Boolean result) { 
     pd.dismiss(); 

     // PUT YOUR CODE THAT YOU WANT TO RUN AFTER THE DATA HAS LOADED HERE! 
} 
0

コード実行をロックしないことは、AsyncTaskのポイントです。コード実行をブロックする場合は、メインスレッドでProgressDialogを作成するだけです

関連する問題