在Realm Recycler中搜索视图查看Android

时间:2017-08-08 13:20:22

标签: android search android-recyclerview recycler-adapter realmsearchviewcontroller

我已在我的应用中实施了回收站视图,以显示公司中的人员列表。我将所有这些信息存储在Realm DB中。现在我想在窗口顶部实现搜索选项,以便用户可以按名称搜索某个人。我尝试过以下代码。但是这个功能不起作用。我无法在这个窗口上搜索任何内容。这是我的recyclerView searchView代码。

我的活动课

undefined

我的适配器类

public class MyColleaguesPage extends AppCompatActivity implements MyColleaguesAdapter.ColleagueListListener{

private RecyclerView recyclerView;
private MyColleaguesAdapter adapter;
private Realm colleagueRealm;
private RealmResults<MyColleagueModel> colleagueResult;
private List<MyColleagueModel> filteredModelList;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mycolleagues_layout);

    }
    colleagueRealm = Realm.getDefaultInstance();
    recyclerView = (RecyclerView) findViewById(R.id.colleagues_recycler);

    if (!Prefs.with(this).getPreLoadColleague()) {
        setRealmData();
    }
    setUpRecycler();

    showAllPersons();

}

private void showAllPersons() {
    colleagueResult = colleagueRealm.where(MyColleagueModel.class).findAll();
    setAdapter(colleagueResult);
    adapter.notifyDataSetChanged();
}

private void setAdapter(RealmResults<MyColleagueModel> results) {

    adapter = new MyColleaguesAdapter(this, colleagueRealm.where(MyColleagueModel.class).findAllSortedAsync("id"),this);
    recyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
}
private void setUpRecycler() {
    // use this setting to improve performance if you know that changes
    // in content do not change the layout size of the RecyclerView
    recyclerView.setHasFixedSize(true);
    // use a linear layout manager since the cards are vertically scrollable
    final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(layoutManager);
}

private void setRealmData(){

    .......
}

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

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

private void search(SearchView searchView) {

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {

            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {

            filteredModelList = filter(colleagueResult, newText);

            if (filteredModelList.size() > 0) {
                return true;
            } else {
                Toast.makeText(MyColleaguesPage.this, "Not Found", Toast.LENGTH_SHORT).show();
                return false;
            }
        }
    });
}
private List<MyColleagueModel> filter(List<MyColleagueModel> models, String query) {
    query = query.toLowerCase();

    filteredModelList = new ArrayList<>();

    for (MyColleagueModel model : models) {
        final String text = model.getName().toLowerCase().toString();
        if (text.contains(query)) {
            filteredModelList.add(model);
        }
    }

    // arraylist in your adapter
    adapter = new MyColleaguesAdapter(this,colleagueRealm.where(MyColleagueModel.class).findAllSortedAsync("name"), getApplicationContext());
    recyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();

    return filteredModelList;
  }
}

1 个答案:

答案 0 :(得分:1)

  

你的代码看起来不错只是一个错误

     

您正在过滤结果并存储如下

    filteredModelList = new ArrayList<>();

    for (MyColleagueModel model : models) {
        final String text = model.getName().toLowerCase().toString();
        if (text.contains(query)) {
            filteredModelList.add(model);
        }
    }

  

但将数据旧数据从领域传递到适配器,而不是过滤数据

// arraylist in your adapter
    adapter = new MyColleaguesAdapter(this,colleagueRealm.where(MyColleagueModel.class).findAllSortedAsync("name"), getApplicationContext());
    recyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();

  

尝试这可能有效

adapter = new MyColleaguesAdapter(this,
colleagueRealm.where(MyColleagueModel.class).contains("fieldToQueryBy", query, Case.INSENSITIVE).findAll(), getApplicationContext());
    recyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();

  

还有一件事是使用ignoreCase而不是toLowercase来实现更好的匹配

     

希望有所帮助