简体   繁体   English

Android SQL多重搜索

[英]Android sql multiple search

I create database that can be search by single string(Name), and now i need to search by multiple strings that is separated by comma, and here I am stuck. 我创建了可以通过单个字符串(名称)进行搜索的数据库,现在我需要通过以逗号分隔的多个字符串进行搜索,在这里我被困住了。 Example how i want to look like: Mark,John,Mia .... Anybody know how to add that to this code? 我想要的示例示例:Mark,John,Mia ....有人知道如何将其添加到此代码中吗?

TestListActivity TestListActivity

@Override
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.test_search, menu);

        MenuItem search = menu.findItem(R.id.search);
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(search);
        searchView.setOnQueryTextListener(this);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onQueryTextSubmit(String query){
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText){
        newText = newText.toLowerCase();
        ArrayList<Test> newList = new ArrayList<>();
        for (Test test : listTest){
            String name = test.getName().toLowerCase();
            if (name.contains(newText)){
                newList.add(test);
            }
        }

        testRecyclerAdapter.setFilter(newList);
        return true;
    }

TestRecyclerAdapter TestRecyclerAdapter

public void setFilter (ArrayList<Test> newList){
    listTest = new ArrayList<>();
    listTest.addAll(newList);
    notifyDataSetChanged();
}

This is how it looks right now RecyclerSearch 这就是现在的样子RecyclerSearch

Update your onQueryTextChange method to this: 将您的onQueryTextChange方法更新为此:

public boolean onQueryTextChange(String newText){
        newText = newText.toLowerCase();
        String[] splitter = newText.split(","); //splits the query into array by ',' 
        ArrayList<Test> newList = new ArrayList<>();
        for (Test test : listTest){
            boolean contain = false;
            String name = test.getName().toLowerCase();
            for(int i = 0; i < splitter.length; i++){
               if (name.contains(splitter[i])){ //contains at least one of the tags
                   contain=true;break;
               }
            }
            if(contain) 
                newList.add(test);
        }

        testRecyclerAdapter.setFilter(newList);
        return true;
    }

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

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