【问题标题】:How to search a item in the dynamic ListView in android?如何在android的动态ListView中搜索项目?
【发布时间】:2014-03-19 12:29:44
【问题描述】:

我正在尝试使用适配器类在项目列表上开发搜索功能。

当listview通过json webservice从php-mysql数据库动态获取所有项目时如何在listview中使用搜索功能。

我已经在link 上发布了我完成的代码。

【问题讨论】:

  • 在此链接中,数据是静态的。我通过 json 和适配器获取数据,用于在列表视图中绑定数据
  • 它与静态或动态数据无关。因为您在适配器上应用了过滤器。因此,您的适配器包含的任何数据都将适用于此。
  • 您是否按照教程进行操作?
  • 我按照教程进行操作,但我在 onTextChanged 方法中发现了错误,在这一行->> AddToOutlet.this.adapter.getFilter().filter(cs);

标签: android android-listview android-service android-softkeyboard android-search


【解决方案1】:

Arraylist 中的所有数据只需在活动中实现 TextWatcher 并在 onTextchanged 中调用 filterlist 方法。

 @Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    // TODO Auto-generated method stub
    adp.filterList(s);
}
// add this method in adapter
    public emotions_adapter(Context context, ArrayList<String> emotions_symbol,
        ArrayList<String> emotions_name) {
    emo_symbole = emotions_symbol;
    emo_name = emotions_name;
    this.response_name = emotions_name;
    this.response_symbol = emotions_symbol;
    C = context;
    l_Inflater = LayoutInflater.from(context);

}

public void filterList(CharSequence s) {
    if (s.toString().length() > 0) {
        ArrayList<String> filteredname = new ArrayList<String>();
        ArrayList<String> filteredsymbole = new ArrayList<String>();

        for (int i = 0; i < response_name.size(); i++) {

            if (response_name.get(i).toLowerCase().contains(s.toString().toLowerCase())|| response_symbol.get(i).toLowerCase()
                            .contains(s.toString().toLowerCase())) {

                filteredname.add(response_name.get(i));
                filteredsymbole.add(response_symbol.get(i));
            }
        }

        emo_name = filteredname;
        emo_symbole = filteredsymbole;
    } else {
        emo_name = response_name;
        emo_symbole = response_symbol;

    }
notifyDataSetChanged();
}

【讨论】:

    猜你喜欢
    • 2013-01-28
    • 2012-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多