2017-07-30 90 views
0

我有這個功能,它檢查用戶的選擇是什麼。 因此,例如 是有4種選擇:Android調用多種方法1 by 1

  1. InfoOfUp
  2. InfoOfArt
  3. InfoOfParish
  4. InfoOfAteneo。

因此,當用戶選擇InfoOfUp和InfoOfArt然後在下一個活動中,我將點擊一個按鈕,其中包含function:selected()它將檢查用戶選擇的項目。如果用戶選擇InfoOfUp項目,它將運行特定功能,如果用戶選擇InfoOfArt項目,它也將運行特定功能

問題是每個項目都有自己的功能,並且每個項目都有進度對話框,用於標記功能已經完成或沒有完成。 因此,用戶選擇2個項目有一個錯誤,因爲有2個函數被同時調用;

我想要函數調用1by1函數等待另一個函數完成。

爲了避免混淆,我將函數調用。

public void selected() { 

    if (InfoOfUp.select == 1) { 
       if (ayala == 0) { 
        ayala(); 
       ayala = 1; 
      } else if (ayala == 1) { 

      } 
     } 

     if (InfoOfArt.select == 1) { 
      if (art == 0) { 
       ArtInIsland(); 

       art = 1; 
      } else if (art == 1) { 

      } 
     } 

     if (InfoOfParish.select == 1) { 
      if (parish == 0) { 
       parish(); 
       parish = 1; 

      } else if (parish == 1) { 

      } 
     } 


     if (InfoOfAteneo.select == 1) { 

      if (ateneo == 0) { 
       ateneogallery(); 
       ateneo = 1; 
      } else if (ateneo == 1) { 

      } 

     } 

此外,如果函數調用,它將運行一個asynctask來獲取數據。 這裏是我的AsyncTask:

public class connectAsyncTask3 extends AsyncTask<Void, Void, String> { 
    private ProgressDialog progressDialog; 
    private traffic traffic; 
    private boolean displayDestinationDetails; 
    String url; 
    boolean launchDestination; 

    connectAsyncTask3(String urlPass, traffic traffic, boolean displayDestinationDetails) { 
     this.url = urlPass; 
     this.traffic = traffic; 
     this.displayDestinationDetails = displayDestinationDetails; 
    } 

    @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 
     try { 
      super.onPreExecute(); 
      progressDialog = new ProgressDialog(traffic.this); 
      progressDialog.setMessage("Fetching route, Please wait..."); 
      progressDialog.setIndeterminate(true); 
      progressDialog.show(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    protected String doInBackground(Void... params) { 
     JSONParser jParser = new JSONParser(); 
     String json = jParser.getJSONFromUrl(url); 
     return json; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 
     progressDialog.hide(); 
     if (result != null) { 
      Log.d("momo2", " : " + result); 
      traffic.drawPath(result); 
      speakOut(); 


     } 

     if (displayDestinationDetails) { 

      Intent i = new Intent(traffic.this, poppers.class); 
      i.putExtra("currentMarker", traffic.markers.size()); 
      traffic.startActivity(i); 


     } 






    } 
} 
+0

使該方法'synchronized'是不夠的? –

+0

@ j.e.gr我不是familliar與同步。 –

回答

0

經典的多線程的情況。 創建兩個線程,每一個在方法有關,開始他們並使用

的Thread.join() 開始後,才第一次完成了第二個線程。

great example here

+0

那麼每個方法都應該有一個線程? –

+0

我不知道你的項目是如何構建的,以確保你的線程必須使用相同的方法,但看看我在鏈接中給出的例子,以獲得如何實現多線程的外觀和感覺加入()。 但主要原則是要知道,這種情況必須由線程處理,並讓他們逐個運行 – BNAndroid

+0

編輯我的帖子,對不起。添加了新的代碼 –