2012-04-11 103 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问题,不过这是在我的情况没有别的工作的唯一途径。希望它可以帮助 谢谢