2011-09-12 55 views
1

我需要按順序進行一些驗證,其中一些涉及複雜的數據庫操作。Android - 如何進行序列驗證

所以,我需要在一個單獨的線程UI線程中做到這一點,好嗎? 但是有些驗證會向用戶顯示消息,需要確認什麼,當用戶確認時,應該調用下一個驗證。

此代碼示例解釋我想實現的東西:

void makeValidation1(){ 

    if(condition1Valid()){ 
     makeValidation2(); 

    }else{ 
     DialogInterface.OnClickListener onClick = new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       makeValidation2(); 
      } 
     }; 

     AlertDialog.Builder builder = new AlertDialog.Builder(this) 
     .setMessage("really want to do this?") 
     .setPositiveButton("Yes", onClick); 

     builder.create().show(); 
    } 
} 

void makeValidation2(){ 

    if(condition2Valid()){ 
    }else{ 
     //... 
    } 
} 

boolean condition1Valid() { 
    // complex database Operations 
    return false; 
} 
boolean condition2Valid() { 
    //complex database Operations 
    return false; 
} 

    //... 


    void makeValidation9(){ 
     //... 
    } 

我的問題是:什麼是最好的方法/模式來實現這一點?

1 - 爲每個驗證創建一個asyncTask? (我不能創建只有一個AsyncTask,因爲確認消息可以停止流量)。

2 - 爲每個驗證創建一個Runnable,並在需要調用下一個驗證時創建要運行的線程?

3 - ???


編輯 我測試此代碼@BinaryBazooka,但是,這不是工作。任何幫助?

公共類MainActivity擴展活動實現OnClickListener {

Thread mThread; 
ProgressDialog mProgressDialog; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    Button button = new Button(this); 
    button.setText("Start"); 
    setContentView(button, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 
    button.setOnClickListener(this); 

} 

@Override 
public void onClick(View v) { 
    mThread = new Thread(new Runnable() { 
     @Override 
     public void run() { 
      validations(); 
     } 
    }); 

    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setMessage("Start Thread?"); 
    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.dismiss(); 

      mProgressDialog = new ProgressDialog(MainActivity.this); 
      mProgressDialog.show(); 

      mThread.run(); 
     } 
    }); 

    builder.create().show(); 
} 

void validations(){ 
    //this method go on separated thread 
    validation1(); 

    validation2(); 

    validation3(); 
} 

void validation1(){ 

    if(true){ 
     final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 

     builder.setMessage("Validation 1 failed. Go validation 2?"); 

     builder.setPositiveButton("Go", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       dialog.dismiss(); 
       mProgressDialog.show(); 
       //if user confirm, continue validation thread 
       mThread.notify(); 
      } 
     }); 
     builder.setOnCancelListener(new OnCancelListener() { 

      @Override 
      public void onCancel(DialogInterface dialog) { 
       //if user cancel, stop validation thread 
       mThread.interrupt(); 
      } 
     }); 

     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       mProgressDialog.hide(); 
       builder.create().show(); 
      } 
     }); 

     try { 
      synchronized (mThread) { 
       //wait for user confirmation 
       mThread.wait(); 
      } 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

private void validation2() { 

    if(true){ 
     final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 

     builder.setMessage("validacao 2 failed. Go validation 3?"); 
     builder.setPositiveButton("Go", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       dialog.dismiss(); 
       mProgressDialog.show(); 
       mThread.notify(); 
      } 
     }); 
     builder.setOnCancelListener(new OnCancelListener() { 

      @Override 
      public void onCancel(DialogInterface dialog) { 
       mThread.interrupt(); 
      } 
     }); 

     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       mProgressDialog.hide(); 
       builder.create().show(); 
      } 
     }); 

     try { 
      synchronized (mThread) { 
       mThread.wait(); 
      } 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 

} 

private void validation3() { 
    Log.i("TAG", "<<<<<<<<<< >>>>>>>>>>>>"); 
    runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      Toast.makeText(MainActivity.this, "finished", Toast.LENGTH_SHORT); 
     } 
    }); 
} 

}

回答

0

我會創建一個新的線程,並在這些對話通話睡了吧,你可以直接在你的可運行與訪問UI。 。

runOnUiThread(new Runnable() {       

public void run() {} 

}); 

因此,像..

Thread someThread = new Thread(new Runnable() { 

    @Override 
    public void run(){ 
     runOnUiThread(new Runnable() {       
      public void run() 
      { 
       AlertDialog.Builder builder = new AlertDialog.Builder(context);        
       builder.setMessage(R.string.msg); 
       builder.setPositiveButton(R.string.btn_ok, new DialogInterface.OnClickListener() {     
        @Override 
        public void onClick(DialogInterface dialog, int id) { 
         dialog.dismiss(); 
         someThread.notify(); 
        } 
       }); 
       AlertDialog alert = builder.create();  
       alert.show(); 
      } 
     }); 
    } 
    someThread.wait(); 
+0

泰。你的想法很有幫助 – Rodrigo

0

適用於AsyncTask。 TY。

代碼:

public class MainActivity extends Activity implements OnClickListener { 

//Thread mThread; 
ProgressDialog mProgressDialog; 
private ValidationsAsyncTask async; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    Button button = new Button(this); 
    button.setText("Start"); 
    setContentView(button, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 
    button.setOnClickListener(this); 

} 

@Override 
public void onClick(View v) { 


    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setMessage("Start Thread?"); 
    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 


     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.dismiss(); 

      mProgressDialog = new ProgressDialog(MainActivity.this); 
      mProgressDialog.show(); 

      async = new ValidationsAsyncTask(); 
      async.execute(); 
     } 
    }); 

    builder.create().show(); 
} 

void validation1(){ 

    if(true){ 
     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 

       builder.setMessage("Validation 1 failed. Go validation 2?"); 

       builder.setPositiveButton("Go", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.dismiss(); 
         mProgressDialog.show(); 

         //if user confirm, continue validation thread 
         synchronized (async) { 
          async.notify(); 
         } 
        } 
       }); 
       builder.setOnCancelListener(new OnCancelListener() { 

        @Override 
        public void onCancel(DialogInterface dialog) { 
         //if user cancel, stop validation thread 
         async.cancel(true); 
        } 
       }); 
       mProgressDialog.hide(); 
       builder.create().show(); 
      } 
     }); 

     Log.i("TAG - validation1", Thread.currentThread().getName()); 

     try { 
      synchronized (async) { 
       //wait for user confirmation 
       async.wait(); 
      } 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

private void validation2() { 

    if(true){ 
     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 

       builder.setMessage("validacao 2 failed. Go validation 3?"); 
       builder.setPositiveButton("Go", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.dismiss(); 
         mProgressDialog.show(); 
         synchronized (async) { 
          async.notify(); 
         } 
        } 
       }); 
       builder.setOnCancelListener(new OnCancelListener() { 

        @Override 
        public void onCancel(DialogInterface dialog) { 
         async.cancel(true); 
        } 
       }); 

       mProgressDialog.hide(); 
       builder.create().show(); 
      } 
     }); 

     try { 
      synchronized (async) { 
       async.wait(); 
      } 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 

} 

private void validation3() { 
    runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      mProgressDialog.dismiss(); 
      Toast.makeText(MainActivity.this, "finished", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 

class ValidationsAsyncTask extends AsyncTask<Void, Void, Void>{ 

    @Override 
    protected Void doInBackground(Void... params) { 
     validation1(); 

     validation2(); 

     validation3(); 

     return null; 
    } 

    @Override 
    protected void onCancelled() { 
     Toast.makeText(MainActivity.this, "cancelled", Toast.LENGTH_LONG).show(); 
    } 
} 

}