简体   繁体   English

在 android 的应用程序内快速搜索?

[英]quick search within app in android?

I have a application already working fine.我有一个应用程序已经运行良好。 I need to implement a quick search feature to it.我需要对其实施快速搜索功能。 Quick search i mean as the users types in every single character i want to result to be fetched.快速搜索我的意思是当用户输入我想要获取的每个字符时。 My Activity is a list activity for which the data is coming from the database query.我的活动是一个列表活动,其数据来自数据库查询。 Say the list view has 50 items, and when the user searches with word as "test" I want to query the database and filter the items and display it in the same list view.假设列表视图有 50 个项目,当用户使用单词作为“测试”进行搜索时,我想查询数据库并过滤项目并将其显示在同一个列表视图中。 Something like the contact search in android.类似于 android 中的联系人搜索。 Please let me know how to implement this.请让我知道如何实现这一点。 A quick sample would be help full.一个快速的样本将有助于充分。 Thank you.谢谢你。

I implemented this feature in one of my previous app.我在我以前的一个应用程序中实现了这个功能。 The entire code is too long to be pasted here.整个代码太长,这里就不贴了。 So, I have posted some portions of it.所以,我已经发布了它的一些部分。 You may read a bit about the approach used by me and get it done for your app.您可能会阅读一些关于我使用的方法的信息,并为您的应用程序完成它。

EditText filterText;

words = new MySimpleCursorAdapter(Main.this, R.layout.emptylist, temp, from, to);

        filterText= (EditText) findViewById(R.id.search_box);
        filterText.addTextChangedListener(filterTextWatcher);


        words.setFilterQueryProvider(new FilterQueryProvider() {
            @Override
            public Cursor runQuery(CharSequence constraint) {
                Cursor cur=mDbHelper.fetchSugNotes(filterText.getText().toString());
                return cur;
            }
        });


private TextWatcher filterTextWatcher = new TextWatcher() {

    public void afterTextChanged(android.text.Editable s) {

    };

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

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        words.getFilter().filter(s.toString());
    };
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM