在我的Android应用程序中实现搜索(edittext)的步骤

时间:2014-03-12 05:01:26

标签: android

我是开发Android app的新手。在我的应用程序中,我有一个listview,显示存储在SQLite中的项目,现在我需要实现一个搜索,它将从SQLite数据库中搜索和检索项目并显示在listview中。

2 个答案:

答案 0 :(得分:0)

你必须从编辑文本中获取文本,如

Edittext.addTextChangedListener(new TextWatcher() {

                    @Override
                    public void onTextChanged(CharSequence s, int start,
                            int before, int count) {
                        // TODO Auto-generated method stub
                get text over here 

                    @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

                    }


    });

然后在数据库中搜索该文本,然后再次设置listview的适配器,您的问题就解决了 如果您正在使用,则必须清除数组列表或列表,然后再次添加值,然后更新listview:)

答案 1 :(得分:0)

你有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;

        notifyDataSetChanged();
    } else {
        emo_name = response_name;
        emo_symbole = response_symbol;
        notifyDataSetChanged();
    }
}