2014-01-29 85 views
1

我的佈局中有一個AutoCompleteTextView。當用戶輸入「@」字符時,我必須向他們展示一些建議。它通常命名我從互聯網上得到它。建議沒有顯示在AutoCompleteTextView

我得到的名稱,我創建一個ArrayAdapter,如下所示。

autoCtextView.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, 
       int count) { 
      // TODO Auto-generated method stub 
      String lsatChar = s.toString().substring(s.length()-1,s.length()); 
      if(lsatChar.equals("@")) { 
       ArrayAdapter<String> adapter = new ArrayAdapter<String>(DisplayQuestionDetails.this, 
         android.R.layout.simple_list_item_1, namesLsist); 
       autoCtextView.setAdapter(adapter); 
      } 


     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      // TODO Auto-generated method stub 

     } 
    }); 

但是這些建議並未顯示。我做錯了什麼?請詢問是否需要澄清問題

+0

將autocompletetextview方法的閾值添加到您的代碼中。 –

+0

這裏是回答這個問題:[自動完成文本視圖下拉不顯示](http://stackoverflow.com/a/41806707/4517450) –

回答

3

你錯過autoCtextView.setThreshold(1);

(開始從第一個字工作)

例如演示:

String[] strList={"a","aaa","aabb","b","bbc","cbb","c","cdd","caa","d","ddc","dda","e","eea","ebc","aec"}; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     //Creating the instance of ArrayAdapter containing list 
      ArrayAdapter<String> adapter = new ArrayAdapter<String> 
      (this,android.R.layout.select_dialog_item,strList); 

     //Getting the instance of AutoCompleteTextView 
      AutoCompleteTextView autoCtextView= (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1); 
      autoCtextView.setThreshold(1);   //to start working from first character 
      autoCtextView.setAdapter(adapter);//set the adapter data to the AutoCompleteTextView 




} 
0

這樣做:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(DisplayQuestionDetails.this, android.R.layout.simple_list_item_1, namesLsist); 
autoCtextView.setAdapter(adapter); 

之前 autoCtextView.addTextChangedListener(新TextWatcher(){..

3

聲明autocompleteTextView比填充後e第一個適配器。

Ref here

public class CountriesActivity extends Activity { 
protected void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.countries); 

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_dropdown_item_1line, COUNTRIES); 
    AutoCompleteTextView textView = (AutoCompleteTextView) 
      findViewById(R.id.countries_list); 
    textView.setAdapter(adapter); 
} 

private static final String[] COUNTRIES = new String[] { 
    "Belgium", "France", "Italy", "Germany", "Spain" 
}; 

}

0

我做了定製的自動完成文本框與此blog的幫助。當用戶鍵入「@something」,例如「@ f」,那麼所有匹配'f'的名字都會出現。

CustomAutoComplete.java

public class CustomAutoComplete extends AutoCompleteTextView { 
    private String previous = ""; 
    private String seperator = "@"; 

    public CustomAutoComplete(final Context context, final AttributeSet attrs, final int defStyle) { 
     super(context, attrs, defStyle); 
     this.setThreshold(0); 
    } 

    public CustomAutoComplete(final Context context, final AttributeSet attrs) { 
     super(context, attrs); 
     this.setThreshold(0); 
    } 

    public CustomAutoComplete(final Context context) { 
     super(context); 
     this.setThreshold(0); 
    } 

    /** 
    * This method filters out the existing text till the separator 
    * and launched the filtering process again 
    */ 
    @Override 
    protected void performFiltering(final CharSequence text, final int keyCode) { 
     String filterText = text.toString().trim(); 
     previous = filterText.substring(0, filterText.lastIndexOf(getSeperator()) + 1); 
     filterText = filterText.substring(filterText.lastIndexOf(getSeperator()) + 1); 
     if (!TextUtils.isEmpty(filterText)) { 
      super.performFiltering(filterText, keyCode); 
     } 
    } 

    /** 
    * After a selection, capture the new value and append to the existing 
    * text 
    */ 
    @Override 
    protected void replaceText(final CharSequence text) { 
     super.replaceText(previous + text + getSeperator()); 
    } 

    public String getSeperator() { 
     return seperator; 
    } 

} 

版式文件:

<com.example.app.CustomAutoComplete 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/autoCompleteTextView" 
     android:layout_gravity="center_horizontal|top" /> 

MainActivity:

public class MainActivity extends ActionBarActivity { 
    CustomAutoComplete autoCompleteTextView; 
    String[] namesLsist = {"zivame","ziooo","zoooO","flipme","flipkart"}; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     autoCompleteTextView = (CustomAutoComplete) findViewById(R.id.autoCompleteTextView); 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, 
       android.R.layout.simple_list_item_1, namesLsist); 
     autoCompleteTextView.setAdapter(adapter); 
} 
} 
+0

@ user965071如果您發現比這個答案更好的東西,請發佈它。 –

2

在我的片段佈局,如果我不使用EMS屬性(字段大小以「m」爲單位),建議將不會顯示。當它出現時,就會顯示這些建議。

<AutoCompleteTextView 
     android:id="@+id/enter_activities_text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="100dp" 
     android:ems="10" 
     android:completionThreshold="1"/> 
相關問題