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

在我的android应用程序中实现搜索(edittext)的步骤,android,Android,我是开发android应用程序的新手。在我的应用程序中,我有一个列表视图,可以显示存储在SQLite中的项目,现在我需要实现一个搜索,从SQLite数据库中搜索和检索项目,并显示在列表视图中。那么你必须从编辑文本中获取文本,如 Edittext.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSeq

我是开发android应用程序的新手。在我的应用程序中,我有一个列表视图,可以显示存储在SQLite中的项目,现在我需要实现一个搜索,从SQLite数据库中搜索和检索项目,并显示在列表视图中。

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

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:

Arraylist中的所有数据只需在活动中实现TextWatcher,并在ContextChanged中调用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();
    }
}

检索数组列表中的所有数据。然后在数组列表中搜索您要搜索的内容。不要每次都对sqlite进行查询以进行搜索。谢谢您的提示。。。由于我是初学者,如果您能为我提供一个示例或教程,那就太好了。谢谢@Egor,但我想使用edittext而不是searchview实现搜索功能:searchview只是一个UI元素,它提供了一组方便的控件,使您更容易实现搜索。如果要使用EditText,只需手动实现UI和回调,但搜索机制集成保持不变。在getview中使用emo_name和emo_symbole updated array list来设置数据。