我有一个问题,当我在搜索视图中搜索某个项目时,我能够搜索该项目(对象),但是当我清除文本时(比如取消搜索,当没有文本时),列表显示我之前搜索的同一个对象,在取消后没有显示所有对象。
我试过了:
适配器类:
public void animateTo(List<OrderPogo> models) {
applyAndAnimateRemovals(models);
applyAndAnimateAdditions(models);
applyAndAnimateMovedItems(models);
}
private void applyAndAnimateRemovals(List<OrderPogo> newModels) {
for (int i = items.size() - 1; i >= 0; i--) {
final OrderPogo model = items.get(i);
if (!newModels.contains(model)) {
removeItem(i);
}
}
}
private void applyAndAnimateAdditions(List<OrderPogo> newModels) {
for (int i = 0, count = newModels.size(); i < count; i++) {
final OrderPogo model = newModels.get(i);
if (!items.contains(model)) {
addItem(i, model);
}
}
}
private void applyAndAnimateMovedItems(List<OrderPogo> newModels) {
for (int toPosition = newModels.size() - 1; toPosition >= 0; toPosition--) {
final OrderPogo model = newModels.get(toPosition);
final int fromPosition = items.indexOf(model);
if (fromPosition >= 0 && fromPosition != toPosition) {
moveItem(fromPosition, toPosition);
}
}
}
public OrderPogo removeItem(int position) {
final OrderPogo model = items.remove(position);
notifyItemRemoved(position);
return model;
}
public void addItem(int position, OrderPogo model) {
items.add(position, model);
notifyItemInserted(position);
}
public void moveItem(int fromPosition, int toPosition) {
final OrderPogo model = items.remove(fromPosition);
items.add(toPosition, model);
notifyItemMoved(fromPosition, toPosition);
}
public class CustomFilter extends Filter {
ArrayList<OrderPogo> arrayList;
MaterialRecylerAdapter adapter;
ArrayList<OrderPogo> filterArrayList;
public CustomFilter(ArrayList<OrderPogo> arrayList, MaterialRecylerAdapter adapter) {
this.arrayList = arrayList;
this.adapter = adapter;
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
if(filterArrayList!=null && filterArrayList.size()>0){
Log.e("filterArrayList",""+filterArrayList.size());
// filterArrayList.addAll(arrayList);
}
FilterResults filterResults=new FilterResults();
if(constraint!=null && constraint.length()>0){
constraint=constraint.toString().toUpperCase();
for(int i=0;i<arrayList.size();i++){
if(arrayList.get(i).getId().toUpperCase().contains(constraint)){
filterArrayList.add(arrayList.get(i));
}else if(arrayList.get(i).getLoadingLocation().toUpperCase().contains(constraint)){
filterArrayList.add(arrayList.get(i));
}else if(arrayList.get(i).getUnloadingLocation().toUpperCase().contains(constraint)){
filterArrayList.add(arrayList.get(i));
}else if(arrayList.get(i).getTruckNumberLrNmbr().toUpperCase().contains(constraint)){
filterArrayList.add(arrayList.get(i));
}else if(arrayList.get(i).getDistance().toUpperCase().contains(constraint)){
filterArrayList.add(arrayList.get(i));
}else if(arrayList.get(i).getDate().toUpperCase().contains(constraint)){
filterArrayList.add(arrayList.get(i));
}
}
filterResults.values = filterArrayList;
filterResults.count = filterArrayList.size();
}else{
filterResults.values = items;
filterResults.count = items.size();
// filterArrayList.addAll(arrayList);
// filterResults.count= arrayList.size();
// filterResults.values=arrayList;
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
//Here i want the arraylist from the recylerview without making the arraylist static
items= (List<OrderPogo>) results.values;
notifyDataSetChanged();
}
}
包含SearchView的片段类:
@Override
public boolean onQueryTextChange(String newText) {
if(TextUtils.isEmpty(newText)){
try{
materialRecylerAdapter.getFilter().filter("");
Log.i("Nomad", "onQueryTextChange Empty String");
// placesListView.clearTextFilter();
// materialRecylerAdapter = new MaterialRecylerAdapter(getActivity(), (ArrayList<OrderPogo>) myOrderArrayList,nextFragmentFlag,"default");
// materialRecylerAdapter.notifyDataSetChanged();
// materialRecylerAdapter.getFilter().filter(newText);
}catch(Exception e){
}
}else{
// materialRecylerAdapter.getFilter().filter(newText.toString());
final List<OrderPogo> filteredModelList = filter(myOrderArrayList, newText);
materialRecylerAdapter.animateTo(filteredModelList);
recyclerView.scrollToPosition(0);
}
return true;
}
});
片段中的过滤方法:
private List<OrderPogo> filter(List<OrderPogo> models, String query) {
query = query.toLowerCase();
final List<OrderPogo> filteredModelList = new ArrayList<>();
for (OrderPogo model : models) {
final String text = model.getId().toLowerCase();
final String text1 = model.getLoadingLocation().toLowerCase();
final String text2 = model.getUnloadingLocation().toLowerCase();
final String text3= model.getDistance().toLowerCase();
if (text.contains(query)) {
filteredModelList.add(model);
}else if(text1.contains(query)){
filteredModelList.add(model);
}else if(text2.contains(query)){
filteredModelList.add(model);
}else if(text3.contains(query)){
filteredModelList.add(model);
}
}
return filteredModelList;
}
答案 0 :(得分:1)
您只需在搜索栏中添加对空字段的检查。当查询为空时,将所有元素添加回列表中,并通知数据集更改。
@Override
public boolean onQueryTextChange(String newText) {
initList(); //reset data at the start
if (newText.equals(""))
initList(); //reset data when there is no query
...
initList
方法将初始化所有列表项,并将适配器链接到它。在notifyDataSetChanged
方法的末尾执行onQueryTextChange
,以便在搜索查询中的每次更改后通知适配器有关已更改的数据集。