【问题标题】:How to implement filter search for listAdapter?如何实现listAdapter的过滤搜索?
【发布时间】:2012-08-12 11:08:00
【问题描述】:

adapter.getFilter().filter(s);嗨,我仔细研究了如何实现过滤器搜索,但我仍然无法正常工作。目前我正在使用带有 hashmap 的 listadapter 来显示 listview。现在我如何使用 listadapter 来做搜索功能?以下是我的代码:

private ListAdapter adapter;
ArrayList<HashMap<String, String>> eventsList;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
      setContentView(R.layout.homepage);
      searchBar.addTextChangedListener(filterTextWatcher);

// HashMap for ListView
eventsList = new ArrayList<HashMap<String, String>>();
adapter = new SimpleAdapter(HomepageActivity.this, eventsList,
R.layout.list_event, new String[] { TAG_EID, TAG_ENAME, TAG_CNAME, TAG_CREATED_AT },
new int[] { R.id.eid, R.id.ename, R.id.cname, R.id.created_at });

// updating listView
setListAdapter(adapter);
}
private TextWatcher filterTextWatcher = new TextWatcher() { 

    public void afterTextChanged(Editable s) { 
    } 

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

    public void onTextChanged(CharSequence s, int start, int before, 
            int count) { 
        adapter.getFilter().filter(s);<--- THIS IS WHERE I GOT ERROR
    } 

}; 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    searchBar.removeTextChangedListener(filterTextWatcher); 
}

【问题讨论】:

  • 尝试使用afterTextChange而不是onTextChanged来过滤来自适配器的文本
  • 我正在使用 listadapter 来帮助我将数据显示到 listview。我不知道如何将 listadapter 更改为 arrayAdapter,我可以在其中使用 getFilter().filter(s),因为这只提供了 arrayAdapter。

标签: android eclipse search filter


【解决方案1】:

简单的简短答案是您必须扩展 ListActivity 而不是 Actity 因为我遇到了同样的问题,我的提示参考是

The method setListAdapter(ArrayAdapter) is undefined for the type ReminderListActivity

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-29
    • 2018-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-25
    相关资源
    最近更新 更多