如何在不使用过滤器的情况下在edittext上键入时显示列表视图?

时间:2013-12-17 13:29:33

标签: android android-listview android-edittext

我创建了一个应用程序,当用户在编辑文本上键入时,它必须显示列表视图。事情是不应该使用过滤器。任何人都可以帮我这个吗?

1 个答案:

答案 0 :(得分:2)

//imports

public class SearchActivity extends Activity implements TextWatcher{
  private EditText searchField;
  private SearchActivity activity;
  private ListView listView;

  protect void onCreate(Bundle savedInstances){
    setContentView(R.layout.yourlayout);
    activity = this;

    searchField = (EditText)findViewById(R.id.searchField);
    searchField.addTextChangedListener(this);

    listView = (ListView)findViewById(R.id.list);
    listView.setOnItemClickListener(new OnItemClickListener(){
      @Override
      public void onItemClick(Adapter<?> arg0, View arg1, int position, long arg3){
        //action when an item is clicked
      }
    });
  }  

  @Override
  public void afterTextChanged(final Editable searchField){
    listView.setAdapter(null); //refresh list items

    if(searchField.length()>0){
      //do your searches here
      listView.setAdapter(new YourCustomAdapter());
    }
  }     

  @Override
  public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}

  @Override
  public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}

}