Android中基于用户输入的过滤列表视图?

Android中基于用户输入的过滤列表视图?,android,Android,我需要根据用户在编辑文本字段中的输入筛选列表视图。如果用户输入字母“a”,我需要显示以“a”开头的列表项。有人能给我一些解决方法吗?提前谢谢 void searchEngine(final String[] strArr){ LinearLayout llay=new LinearLayout(this); llay.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PA

我需要根据用户在编辑文本字段中的输入筛选列表视图。如果用户输入字母“a”,我需要显示以“a”开头的列表项。有人能给我一些解决方法吗?提前谢谢

void searchEngine(final String[] strArr){
      LinearLayout llay=new LinearLayout(this);
      llay.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));        
      llay.setOrientation(LinearLayout.VERTICAL);

      final ListView lv=new ListView(this);        
      lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked, strArr));      


      final EditText ed=new EditText(searcher.this);
      ed.setWidth(80);
      ed.setOnEditorActionListener(new OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            // TODO Auto-generated method stub
             lv.setTextFilterEnabled(true);
             String strfilter=ed.getText().toString();
             lv.setFilterText(strfilter);

            return false;
        }
    });

      llay.addView(ed);
      llay.addView(lv);


    setContentView(llay);

}
onEditorAction不是在输入一个字符后调用,而是在一些操作(例如按Enter键)后调用。试着用它来代替

ed.addTextChangedListener(new TextWatcher() {
    @Override
    public void afterTextChanged(Editable s) {
        lv.setFilterText(s.toString());
    }

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

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) { }
});

这是您正在寻找的:。用ListView中的项目填充对它的建议,当您键入时,它将被过滤。最好过滤搜索对话框,而不是过滤列表视图


如果希望列表视图的末尾只有一个项目,请在“搜索”对话框中选取选定的单击项目,并删除列表视图中与之不相等的所有项目

我无法筛选列表视图。在向editText字段键入值时,我需要筛选列表视图。