建议未在AutoCompleteTextView中显示

时间:2014-01-29 09:31:25

标签: android 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

        }
    });

但建议没有显示。我做错了吗?请询问是否需要澄清问题

5 个答案:

答案 0 :(得分:6)

你想念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  




}  

答案 1 :(得分:3)

声明autocompleteTextView后填充第一个适配器。

喜欢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"
 };

}

答案 2 :(得分:2)

在我的片段布局中,如果我不使用ems属性(字段大小为&#34; m&#34; s),则不会显示建议。如果存在,则显示建议。

<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"/>

答案 3 :(得分:0)

这样做:

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

在     autoCtextView.addTextChangedListener(new TextWatcher(){...

答案 4 :(得分: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);
}
}