【发布时间】:2018-11-03 21:58:41
【问题描述】:
我正在尝试使用 SearchView 实现一个简单的搜索按钮,以在 RecyclerView 中查找项目。但是,这个搜索按钮会破坏我的排序功能,因为在 SearchView 中键入后 RecyclerView 无法再排序。
这是我的活动
private void sortAscending() {
Collections.sort(stationList, (station1, station2) -> {
if (station1.getData().getCurrent().getPollution().getAqius() > station2.getData().getCurrent().getPollution().getAqius()) {
return 1;
} else if (station1.getData().getCurrent().getPollution().getAqius() < station2.getData().getCurrent().getPollution().getAqius()) {
return -1;
} else {
return 0;
}
});
}
private void sortDescending() {
Collections.sort(stationList, (station1, station2) -> {
if (station1.getData().getCurrent().getPollution().getAqius() > station2.getData().getCurrent().getPollution().getAqius()) {
return -1;
} else if (station1.getData().getCurrent().getPollution().getAqius() < station2.getData().getCurrent().getPollution().getAqius()) {
return 1;
} else {
return 0;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu here
getMenuInflater().inflate(R.menu.list_menu, menu);
MenuItem searchItem = menu.findItem(R.id.item_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(this);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//A String for the message to be displayed in a Toast
String msg = "";
//Switch and case on the MenuItem object's id
switch (item.getItemId()) {
case R.id.sort_ascending:
msg = "sorting by ascending.";
sortAscending();
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
break;
case R.id.sort_descending:
msg = "sorting by descending.";
sortDescending();
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
break;
}
adapter.notifyDataSetChanged();
return super.onOptionsItemSelected(item);
}
@Override
public boolean onQueryTextChange(String newText) {
filter(newText.toLowerCase());
return true;
}
private void filter(String text) {
ArrayList<Station> filteredList = new ArrayList<>();
for (Station item : stationList) {
if (item.getData().getCity().toLowerCase().contains(text.toLowerCase())) {
filteredList.add(item);
}
}
adapter.filterList(filteredList);
adapter.notifyDataSetChanged();
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
现在我怀疑它与 onOptionsItemSelected 方法有关,因为当用户点击菜单上的搜索按钮时,我没有任何东西可以做某事。
这是 RecyclerView 的 Adapter 类,为简单起见删除了其他功能。
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
//...set text and information from ArrayList
//Launch an activity and user clicks on an item in RecyclerView
holder.itemView.setOnClickListener(view -> {
Intent intent = new Intent(context, CityActivity.class);
context.startActivity(intent);
});
}
//Function to set the ArrayList to the filtered list
//filteredList ArrayList is passed in from filter() in Activity
public void filterList(ArrayList<Station> filteredList) {
stationList = filteredList;
}
这里是菜单的 XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/item_sort"
android:icon="@drawable/ic_sort_black_24dp"
android:title="Sort"
app:showAsAction="ifRoom">
<menu>
<item android:id="@+id/sort_ascending"
android:title="Sort by ascending"/>
<item android:id="@+id/sort_descending"
android:title="Sort by descending"/>
</menu>
</item>
<item android:id="@+id/item_search"
android:icon="@drawable/ic_search_black_24dp"
android:title="Search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
我无法弄清楚为什么在将信息输入 SearchView 并将其删除以使 RecyclerView 返回其正常状态后,我无法使用排序功能(升序和降序)对 RecyclerView 进行排序。
【问题讨论】:
-
您的排序函数访问哪些属性?
-
哎呀,我忘了在帖子中包含它。我已经编辑了帖子并添加了排序功能。他们只是使用一个比较器,然后在我的对象的 ArrayList 上调用 Collections.sort()。这些对象基于 AirVisual API 数据,这就是为什么它们中有一堆 getter。基本上我比较两个整数值,如果一个值大于另一个值,则为 Comparator 返回 1。
标签: java android sorting android-recyclerview searchview