大写和小写搜索

时间:2019-01-21 17:00:38

标签: java android realm

public class ActivityPS extends AppCompatActivity {
    private PS_DA ps_da = new PS_DA(ActivityPS.this);
    RecyclerView recyclerView;
    android.widget.SearchView searchView;
    Realm realm = Realm.getDefaultInstance();
    AdapterPS adapterPS;
    List<PS> filteredPs;
    RealmResults<PS> psList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ps);
        cast();
        setUpRecycler();
        searchviewSetUp();
    }

    public void searchviewSetUp(){
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                return false;
            }
        });
    }

    @Override
    public boolean onQueryTextChange(String s) {
        filteredPs =filter(psList,s);
        if (filteredPs.size()> 0){
            return true;
        }
        return false;
    }

    public List<PS> filter (List<PS> psLists,String s){
        s = s.toLowerCase();
        filteredPs = new ArrayList<>();
        for (PS psList : psLists){
        final String text = psList.getCommandName().toString().toLowerCase();
        if (text.contains(s));
            filteredPs.add(psList);
        }
        psList = realm.where(PS.class).contains("commandName",s).findAll();
        adapterPS = new AdapterPS(ActivityPS.this,psList,realm);
        recyclerView.setLayoutManager(new LinearLayoutManager(ActivityPS.this));
        recyclerView.setAdapter(adapterPS);

        return filteredPs;
    }
}

我想搜索我的领域数据库,并且希望我的搜索视图不区分大小写。我这样做了,但是无论插入小写还是大写,结果中只显示小写数据。我不知道该怎么办...

3 个答案:

答案 0 :(得分:2)

您可以进行realm DB搜索case insensitive,并在方法中添加Case.INSENSITIVE boolean作为第三argument

public RealmQuery<E> contains(String fieldName, String value, boolean
> caseSensitive)
     

摘自文档:所有四个字符串谓词,即contains, beginsWith, endsWith, like具有可选的第三个谓词   控制区分大小写的参数:Case.INSENSITIVE和   区分大小写。默认值为Case.SENSITIVE。

psList = realm.where(PS.class).contains("commandName",s, Case.INSENSITIVE).findAll();

答案 1 :(得分:1)

我刚刚找到了...。 我将代码更改为此并且可以正常工作。

public class ActivityPS extends AppCompatActivity {
private PS_DA ps_da = new PS_DA(ActivityPS.this);
RecyclerView recyclerView;
android.widget.SearchView searchView;
Realm realm = Realm.getDefaultInstance();
AdapterPS adapterPS;
List<PS> filteredPs;
RealmResults<PS> psList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ps);
    cast();
    setUpRecycler();
    searchviewSetUp();
}

public void searchviewSetUp(){
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {

    return false;
}

@Override
public boolean onQueryTextChange(String s) {

   psList = realm.where(PS.class).contains("commandName",s, Case.INSENSITIVE).findAll();
    adapterPS = new AdapterPS(ActivityPS.this,psList,realm);
    recyclerView.setLayoutManager(new LinearLayoutManager(ActivityPS.this));
    recyclerView.setAdapter(adapterPS);


    return false;
}

.Case.INSENSITIVE是自动完成的。

答案 2 :(得分:0)

将所有结果都转换为小写,因此以后的内容都不敏感。