SearchView无法过滤项目。

huangapple go评论80阅读模式
英文:

SearchView doesn´t filter items

问题

很遗憾,我的SeachView不起作用。在Android Studio中没有显示任何错误,我的应用程序也没有任何问题。我的问题是,当我在SearchView中输入内容时,没有任何反应。RecyclerView仍然显示来自我的ArrayList的所有项目,并且不对它们进行筛选。

主活动:

public class MainActivity extends AppCompatActivity {
    // ... (其他代码)

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

        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);

        list = new ArrayList<>();
        list.addAll(MountainData.getListData());

        showRecyclerViewList();

        mAdapter = new ItemCardViewAdapter(this);
        mAdapter.setListMountain(list);
        
        searchView = (SearchView) findViewById(R.id.searchView);
        searchView.setQueryHint("Search View");

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                mAdapter.getFilter().filter(query);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String query) {
                mAdapter.getFilter().filter(query);
                return false;
            }
        });
        
    }
    // ... (其他代码)

    private void showRecyclerViewList() {
        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        ItemCardViewAdapter cardViewAdapter = new ItemCardViewAdapter(this);
        cardViewAdapter.setListMountain(list);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setAdapter(cardViewAdapter);
    }
}

适配器:

public class ItemCardViewAdapter extends RecyclerView.Adapter<ItemCardViewAdapter.CardViewHolder> implements Filterable {
    // ... (其他代码)

    @Override
    public Filter getFilter(){
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {
                String charString = charSequence.toString();
                if(charString.isEmpty()){
                    filteredList = listMountain;
                }
                else {
                    ArrayList<Mountain> MountainFilteredList = new ArrayList<>();
                    for(Mountain item : listMountain){
                        if(item.getName().toLowerCase().contains(charString.toLowerCase())) {
                            MountainFilteredList.add(item);
                        }
                    }
                    filteredList = MountainFilteredList;
                }
                FilterResults filterResults = new FilterResults();
                filterResults.values = filteredList;
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
                filteredList = (ArrayList<Mountain>) filterResults.values;
                notifyDataSetChanged();
            }
        };
    }
    // ... (其他代码)
}

模型:

public class Mountain implements Parcelable {
    // ... (其他代码)
}
英文:

Unfortunately, my SeachView does not work. No errors are shown in Android Studio and my app works without any problems. My problem is that when I enter something into the SearchView nothing happens. The Recyclerview still shows all items from my Array List and does not filter them.
Thanks for all the answers SearchView无法过滤项目。

Main Activity:

public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ArrayList&lt;Mountain&gt; list;
private SearchView searchView;
private ItemCardViewAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
list = new ArrayList&lt;&gt;();
list.addAll(MountainData.getListData());
showRecyclerViewList();
mAdapter = new ItemCardViewAdapter(this);
mAdapter.setListMountain(list);
searchView = (SearchView) findViewById(R.id.searchView);
searchView.setQueryHint(&quot;Search View&quot;);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
mAdapter.getFilter().filter(query);
return false;
}
@Override
public boolean onQueryTextChange(String query) {
mAdapter.getFilter().filter(query);
return false;
}
});
}
private void showRecyclerViewList() {
recyclerView = findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
ItemCardViewAdapter cardViewAdapter = new ItemCardViewAdapter(this);
cardViewAdapter.setListMountain(list);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setAdapter(cardViewAdapter);
}

Adapter:

public class ItemCardViewAdapter extends RecyclerView.Adapter&lt;ItemCardViewAdapter.CardViewHolder&gt; implements Filterable {
private Context context;
private ArrayList&lt;Mountain&gt; listMountain;
private ArrayList&lt;Mountain&gt; filteredList = new ArrayList&lt;&gt;();
public ItemCardViewAdapter(MainActivity context) {
this.context = context;
}
public ArrayList&lt;Mountain&gt; getListMountain() {
return listMountain;
}
public void setListMountain(ArrayList&lt;Mountain&gt; listMountain) {
this.listMountain = listMountain;
}
@NonNull
@Override
public CardViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cardview, parent, false);
return new CardViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull CardViewHolder holder, final int position) {
holder.tvName.setText(getListMountain().get(position).getName());
holder.tvDescription.setText(getListMountain().get(position).getDescription());
Glide.with(context).load(getListMountain().get(position).getPhoto()).into(holder.imgPhoto);
// intent parcel able to detail
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent detailActivity = new Intent(context, DetailActivity.class);
detailActivity.putExtra(DetailActivity.EXTRA_MOUNTAIN, listMountain.get(position));
context.startActivity(detailActivity);
}
});
}
@Override
public int getItemCount() {
return getListMountain().size();
}
public class CardViewHolder extends RecyclerView.ViewHolder {
ImageView imgPhoto;
TextView tvName, tvDescription;
Button button;
public CardViewHolder(@NonNull View itemView) {
super(itemView);
imgPhoto = itemView.findViewById(R.id.img_card);
tvName = itemView.findViewById(R.id.tv_name_card);
tvDescription = itemView.findViewById(R.id.tv_desc_card);
button = itemView.findViewById(R.id.button);
}
}
@Override
public Filter getFilter(){
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if(charString.isEmpty()){
filteredList = listMountain;
}
else
{
ArrayList&lt;Mountain&gt; MountainFilteredList = new ArrayList&lt;&gt;();
for(Mountain item : listMountain){
if(item.getName().toLowerCase().contains(charString.toLowerCase())) {
MountainFilteredList.add(item);
}
}
filteredList = MountainFilteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = filteredList;
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
filteredList = (ArrayList&lt;Mountain&gt;) filterResults.values;
notifyDataSetChanged();
}
};
}

Model:

public class Mountain implements Parcelable {
// create implementation Parcelable to this class
private String name, elevation, photo, description, location;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getElevation() {
return elevation;
}
public void setElevation(String elevation) {
this.elevation = elevation;
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public Mountain() {
}
protected Mountain(Parcel in) {
name = in.readString();
elevation = in.readString();
photo = in.readString();
description = in.readString();
location = in.readString();
}
public static final Creator&lt;Mountain&gt; CREATOR = new Creator&lt;Mountain&gt;() {
@Override
public Mountain createFromParcel(Parcel in) {
return new Mountain(in);
}
@Override
public Mountain[] newArray(int size) {
return new Mountain[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(name);
parcel.writeString(elevation);
parcel.writeString(photo);
parcel.writeString(description);
parcel.writeString(location);
}

答案1

得分: 0

1) 在ItemCardViewAdapter()的构造函数中,你需要将filteredList传递给你的listMountain列表。

public ItemCardViewAdapter(MainActivity context) {
      this.context = context;
      listMountain = new ArrayList<>();
      listMountain.addAll(filteredList);
}

2) 首先,在publishResults()方法中清除listMountain,然后将filteredList添加到listMountain列表中。

protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
    listMountain.clear();
    listMountain.addAll((Collection<? extends Mountain>) filterResults.values);
    notifyDataSetChanged();
}
英文:

1) In ItemCardViewAdapter() Constructor you need to pass filteredList into your listMountain list.

public ItemCardViewAdapter(MainActivity context) {
this.context = context;
listMountain= new ArrayList&lt;&gt;();
listMountain.addAll(filteredList);
}

2) firstly, clear listMountain and then add filteredList into listMountain list in publishResults() method.

  protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
listMountain.clear();
listMountain.addAll((Collection&lt;? extends Mountain&gt;) filterResults.values;
notifyDataSetChanged();
}

huangapple
  • 本文由 发表于 2020年10月16日 20:17:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64389046.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定