2012-04-11 100 views
1

目的:AutoCompleteTextView不建議只是優先搜索

我想從web服務的一些字符串和填充AutoCompleteTextView與them.This很簡單,但我真正想要的是開始搜索(調用Web服務)輸入完成時。

我的意思是,例如,我輸入了一些東西,輸入完成後3秒鐘,AutoCompleteTextView會被填充並顯示建議。

我做了什麼至今:

正如你可以在下面的代碼中看到的,我用了一個CountDownTimer實現這一目標。我設置了3秒,並在OnTextChanged中啓動它。當用戶輸入時,我清除CountDownTimer並創建一個新的實例並重新啓動它。

因此,無論何時用戶鍵入每個按鍵 - 我重置計數器。

之後,我調用我的方法 - 在CountDownTimer的OnFinish()中調用webservice並填充AutoCompleteTextView。

問題:

當我完成打字,一切正常,我可以在調試模式下看到。但建議不會出現在第一次搜索。

我的意思是,它工作得很好,但AutoCompleteTextView沒有得到與第一次填充數據。

注:

我調用webservice的同步和異步方式。

counter=new MyCount(3000, 1000); 
autoComplete.addTextChangedListener(new TextWatcher(){ 

     public void afterTextChanged(Editable editable) { 

      if(isBackPressed){ //catch from KeyListener, if backspace is pressed don't invoke web service 

       isBackPressed=false; 
       return; 
      } 

      String newText = editable.toString(); 

      searchText=newText; 
     } 

     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 


     } 

     public void onTextChanged(CharSequence s, int start, int before, int count) { 

      if(isBackPressed){ 

       isBackPressed=false; 
       return; 
      } 

      counter.cancel(); 
      counter=new MyCount(3000, 1000); 
      counter.start(); 
     } 

    }); 

這是我的CountDownTimer類

public class MyCount extends CountDownTimer{ 

    public MyCount(long millisInFuture, long countDownInterval) { 

    super(millisInFuture, countDownInterval); 

    } 
    @Override 
    public void onFinish() { 

     invoke_webservice(searchText); 
    } 
    @Override 
    public void onTick(long millisUntilFinished) { 

    } 
    } 

這是我的方法來調用web服務在同步方式

public void invoke_webservice(String key){ 

    try{ 
        . //code to invoke webservice and populate string [] results 
        . 
        . 
    aAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.item,results); 
    autoComplete.setAdapter(aAdapter); 
    aAdapter.notifyDataSetChanged(); 

} 

這是我的方法來調用web服務在異步方式

class getJson extends AsyncTask<String,String,String>{ 

    @Override 
    protected String doInBackground(String... key) { 

     String newText = key[0]; 

        . //code to invoke webservice and populate string [] results 
        . 
        . 


     runOnUiThread(new Runnable(){ 
      public void run(){ 

       aAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.item,results); 
       autoComplete.setAdapter(aAdapter); 
       aAdapter.notifyDataSetChanged(); 
      } 
     }); 

     return null; 
    } 

} 

預先感謝

+0

請不要爲相同的問題添加重複的帖子,合併您的帖子,這樣人們不會浪費他們的時間看和回答這兩個職位。這通常被認爲是垃圾郵件http://stackoverflow.com/questions/10143308/android-autocompletetextview-filter-is-firing-late-for-the-first-search-only – Mayank 2012-04-14 08:35:54

+0

@Mayank上面的鏈接不工作...請檢查 – 2012-09-06 07:29:33

+0

我認爲用戶已刪除他的帖子,這就是爲什麼該鏈接不工作了。他在另一篇文章中也有同樣的問題,所以我只是向他指出,這樣做並不是一個好主意,可以認爲是垃圾郵件。 – Mayank 2012-09-07 06:25:54

回答

0

這裏是你可以做什麼: 我已設置的閾值3

atvSearchPatient.setThreshold(3); 

申請文本的觀察者監聽器自動填充文本視圖象下面這樣:

//here set the text watcher 
    atvSearchPatient.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

     } 

     @Override 
     public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 

     } 

     @Override 
     public void afterTextChanged(Editable editable) { 
      String str = atvSearchPatient.getText().toString().trim(); 
      if (str.length() >2) { 

        searchPatient(str); 

      } 
     } 
    }); 

在您的第一次調用中使用null值創建API,並使用來自autocomplete textview字段的字符串進行第二次和連續調用,如上所述。然後在響應應用適配器象下面這樣:

this.nameList.clear(); 
    if (nameList.size() > 0) { 
     this.nameList.addAll(dataFromResponseList); 
     atvSearchPatient.setAdapter(null); 
     ArrayAdapter<String> adapter = new ArrayAdapter<>(
       this, android.support.v7.appcompat.R.layout.select_dialog_item_material, nameList); 
     atvSearchPatient.setAdapter(adapter); 
     adapter.notifyDataSetChanged(); 
     atvSearchPatient.showDropDown(); 

它是第一次不工作weired問題,不過這是在我的情況沒有別的工作的唯一途徑。希望它可以幫助 謝謝