2011-09-11 19 views
0

我有一個EditText,用戶可以在其中鍵入業務名稱。我也有這樣的EditText這表明用戶對什麼是已經添加到數據庫下一個ListView ...Android高效AsyncTask

<EditText android:id="@+id/txtBusinessName" android:hint="Name of Business" /> 
<ListView android:id="@+id/suggestionList" 
    android:layout_width="fill_parent" android:layout_height="wrap_content"> 
</ListView> 

現在,當用戶類型,我檢查自己輸入關鍵字,在數據庫和檢索什麼必須在ListView中顯示用戶。目前,在每一個關鍵up事件解僱,我呼籲一種新的AsyncTask這樣...

 EditText txtBusinessName = (EditText) findViewById(R.id.txtBusinessName); 
       txtBusinessName.setOnKeyListener(new View.OnKeyListener() { 
        @Override 
        public boolean onKey(View v, int keyCode, KeyEvent event) { 
         if (event.getAction() == KeyEvent.ACTION_UP) { 
          if (v instanceof EditText) { 
           EditText txtBusinessName = ((EditText) v); 

           if (txtBusinessName.length() > 0) { 
            if (suggestionTask != null) { 
            suggestionTask.cancel(true); 
            suggestionTask = null; 
            } 
            suggestionTask = new GetCompaniesByKeywordAsyncTask(
             AddBusinessActivity.this, s); 
            suggestionTask.execute(txtBusinessName.getText() 
             .toString()); 
           } 
          } 
         } 
         return false; 
        } 
       }); 

有沒有一種方法,我只是的AsyncTask的一個實例,並要求它檢索名稱作爲用戶類型EditText上?因爲創建太多的AsyncTask效率不高,最終會出現異常。我將在收到名稱時填充ListView,我可以讓ListView根據其中的內容重新調整自己的大小嗎?

回答

2

爲了製作一個AsyncTask,您需要重構AsyncTask以基於請求隊列運行。該隊列包含您要處理的所有關鍵字。然後,您將在監聽器外部運行此AsyncTask並添加OnKeylistener中的關鍵字。

要更新的ListView,我們將利用onProgressUpdate,將根據結果在doInBackground

對修改的AsyncTask


    @Override 
    protected Integer doInBackground(Void... params) { 
     int errorCode = 0; 

     try { 
      // while running in the context of your activity 
      // you should set this boolean to false once you have leave the activity 
      while(!isRunning){ 
       // blocking call to get the next keyword that is added to the queue 
       String responseData = getNextKeyword(); 

       // once you get the next keyword, you publish the progress 
       // this would be executed in the UI Thread and basically would update the ListView 
       publishProgress(responseData); 
      } 
     } catch(Exception e) { 
      // error handling code that assigns appropriate error code 
     } 

     return errorCode; 

    } 

    @Override 
    protected void onPostExecute(Integer errorCode) { 
     // handle error on UI Thread based on errorCode 
    } 

    @Override 
    protected void onProgressUpdate(String... values) { 
     String searchKeyword = values[0]; 

     // handle the searchKeyword here by updating the listView 
    } 

    /*** 
    * Stub code for illustration only 
    * Get the next keyword from the queue 
    * @return The next keyword in the BlockingQueue 
    */ 
    private String getNextKeyword() { 
     return null; 
    } 

    /*** 
    * Stub code for illustration only 
    * Add new keyword to the queue, this is called from the onKey method 
    * @param keyword 
    */ 
    public void addKeyword(String keyword) { 
     // add the keyword to the queue 
    } 

然後你的代碼rougly修改爲骨架代碼更新的ListView:


// instantiate AsyncTask once 
suggestionTask = new GetCompaniesByKeywordAsyncTask(
     AddBusinessActivity.this, s); 

// run only one AsyncTask that is waiting for any keyword in the queue 
suggestionTask.execute(); 

EditText txtBusinessName = (EditText) findViewById(R.id.txtBusinessName); 
txtBusinessName.setOnKeyListener(new View.OnKeyListener() { 
    @Override 
    public boolean onKey(View v, int keyCode, KeyEvent event) { 
     if (event.getAction() == KeyEvent.ACTION_UP) { 
      if (v instanceof EditText) { 
       EditText txtBusinessName = ((EditText) v); 

       if (txtBusinessName.length() > 0) { 
        // add new keyword to the queue for processing 
        suggestionTask.addKeyword(txtBusinessName.getText() 
         .toString()); 
       } 
      } 
     } 
     return false; 
    } 
}); 
+0

我需要在AsyncTask對象中維護一個隊列集合嗎? – Neutralizer

+0

如果您不打算在AsyncTask對象之外使用隊列,只需將其保存爲AsyncTask的實例變量就可以了。對於隊列你可能想嘗試利用[BlockingQueue](http://developer.android.com/reference/java/util/concurrent/BlockingQueue.html) – momo

+0

好的工作。你知道一旦ListView被填充,我如何根據其內容重新調整大小? – Neutralizer