我有一个搜索栏,可以在我的所有数据库中进行搜索。
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
psList = ps_da.searchBox(s);
adapterPS = new AdapterPS(ActivityPS.this,psList);
recyclerView.setLayoutManager(new LinearLayoutManager(ActivityPS.this));
recyclerView.setAdapter(adapterPS);
return false;
}
,我有一个“警报”对话框,用于过滤显示在recyclerview中的结果。
imgFilter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Dialog dialog = new Dialog(ActivityPS.this);
dialog.setContentView(R.layout.dialog_filter_layout);
Button btnfliter =dialog.findViewById(R.id.btncheckFilter);
CheckBox chkActive = dialog.findViewById(R.id.chkActiveDirectory);
CheckBox chkDNS = dialog.findViewById(R.id.chkDND);
CheckBox chkDHCP = dialog.findViewById(R.id.chkDHCP);
CheckBox chkVPN = dialog.findViewById(R.id.chkVPN);
CheckBox IP = dialog.findViewById(R.id.IP);
RadioGroup radioGroup = dialog.findViewById(R.id.radiogroupk);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rd = group.findViewById(checkedId);
if (rd.isChecked()){
if (rd.getText().equals("Client")){
chkActive.setEnabled(false);
chkDHCP.setEnabled(false);
chkVPN.setEnabled(false);
chkDNS.setEnabled(false);
}else if (rd.getText().equals("All")){
chkActive.setEnabled(true);
chkDHCP.setEnabled(true);
chkVPN.setEnabled(true);
chkDNS.setEnabled(true);
}else if (rd.getText().equals("Server")){
chkActive.setEnabled(true);
chkDHCP.setEnabled(true);
chkVPN.setEnabled(true);
chkDNS.setEnabled(true);
}
}
}
});
btnfliter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!IP.isChecked()){
if (chkActive.isChecked()){
acvtiveDirectory = chkActive.getText().toString();
}if (chkDHCP.isChecked()){
DHCP =chkDHCP.getText().toString();
}if (chkDNS.isChecked()){
DNS = chkDNS.getText().toString();
}if (chkVPN.isChecked()){
VPN = chkVPN.getText().toString();
} psList =ps_da.searchByFilter(acvtiveDirectory,DNS,DHCP,VPN);
adapterPS = new AdapterPS(ActivityPS.this,psList);
recyclerView.setAdapter(adapterPS);
adapterPS.notifyDataSetChanged();
dialog.dismiss();
}else {
psList = ps_da.findAll();
adapterPS = new AdapterPS(ActivityPS.this,psList);
recyclerView.setAdapter(adapterPS);
dialog.dismiss();
}
}
});
dialog.show();
}
});
}
但是当我添加过滤器时,它正确显示在我的recyclerview上,但是当我想在搜索栏中的过滤结果之间进行搜索时,它将在整个数据库中进行搜索。我想在过滤范围内进行搜索。有想法吗?
这就是我的PS对象的领域DA类:
public RealmResults<PS> findAll(){
pslist = myrealm.where(PS.class).findAll();
return pslist;
}
public RealmResults<PS> searchBox(String s){
pslist = myrealm.where(PS.class).contains("commandName",s, Case.INSENSITIVE).findAll();
return pslist;
}
public RealmResults<PS> searchByFilter(String Active,String DNS,String DHCP,String VPN){
pslist = myrealm.where(PS.class).beginGroup().equalTo("commandName",Active)
.or()
.equalTo("commandName",DHCP)
.or()
.equalTo("commandName",DNS)
.or()
.equalTo("commandName",VPN)
.endGroup().findAll();
return pslist;
}
答案 0 :(得分:0)
在onQueryTextChange
中,您每次都创建一个新适配器。
您需要实例化适配器一次,并在其活动中保留其引用,因此您可以为其设置数据并对其调用getFilter().filter("")
以过滤您设置的数据。
要在适配器中使用过滤器,您可以实现Filterable
,但可以通过performFiltering
方法(Filtering Realm object in android)返回结果。
这是一个示例(未测试)
public class MyAdapter extends BaseAdapter<ArticleEntity> implements Filterable{
private RealmResults<ArticleEntity> items;
private RealmResults<ArticleEntity> filteredItems;
private ArticleFilter filter;
public MyAdapter(RealmResults<ArticleEntity> items) {
this.items = items;
filter = new ArticleFilter();
filter.filter("");
}
public void setItems(RealmResults<ArticleEntity> items) {
this.items = items;
filter.filter("");
}
@Override
public int getCount() {
return filteredItems.size();
}
@Override
public ArticleEntity getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public Filter getFilter() {
return filter;
}
private class ArticleFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
return new FilterResults();
}
@Override
@SuppressWarnings("unchecked")
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredItems = items.where()
.equalTo("your_field", "value")
.sort("name")
.findAll();
notifyDataSetChanged();
}
}
}
因此,基于复选框的过滤应在过滤器publishResults
方法中进行。