目前我只能搜索一个字符串字。例如,我需要搜索多个单词,我输入“牛轧糖,甜甜圈”,他们会告诉我这两个?我需要在代码中放置什么才能工作?
DataAdapter.java
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> implements Filterable {
private ArrayList<AndroidVersion> mArrayList;
private ArrayList<AndroidVersion> mFilteredList;
public DataAdapter(ArrayList<AndroidVersion> arrayList) {
mArrayList = arrayList;
mFilteredList = arrayList;
}
@Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_row, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, int i) {
viewHolder.tv_name.setText(mFilteredList.get(i).getName());
viewHolder.tv_version.setText(mFilteredList.get(i).getVer());
viewHolder.tv_api_level.setText(mFilteredList.get(i).getApi());
}
@Override
public int getItemCount() {
return mFilteredList.size();
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
mFilteredList = mArrayList;
} else {
ArrayList<AndroidVersion> filteredList = new ArrayList<>();
for (AndroidVersion androidVersion : mArrayList) {
if (androidVersion.getApi().toLowerCase().contains(charString) || androidVersion.getName().toLowerCase().contains(charString) || androidVersion.getVer().toLowerCase().contains(charString)) {
filteredList.add(androidVersion);
}
}
mFilteredList = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = mFilteredList;
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
mFilteredList = (ArrayList<AndroidVersion>) filterResults.values;
notifyDataSetChanged();
}
};
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView tv_name,tv_version,tv_api_level;
public ViewHolder(View view) {
super(view);
tv_name = (TextView)view.findViewById(R.id.tv_name);
tv_version = (TextView)view.findViewById(R.id.tv_version);
tv_api_level = (TextView)view.findViewById(R.id.tv_api_level);
}
}
答案 0 :(得分:0)
你可以试试这个
if (charString.contains(",")) {
String searchChar[] = charString.split(",");
for (int i = 0; i < searchChar.length; i++) {
if (androidVersion.getApi().toLowerCase().contains(searchChar[i]) || androidVersion
.getName()
.toLowerCase().contains(searchChar[i]) || androidVersion.getVer().toLowerCase()
.contains(searchChar[i])) {
filteredList.add(androidVersion);
}
}
} else {
if (androidVersion.getApi().toLowerCase().contains(charString) || androidVersion.getName()
.toLowerCase().contains(charString) || androidVersion.getVer().toLowerCase()
.contains(charString)) {
filteredList.add(androidVersion);
}
}
替换代码:
if (androidVersion.getApi().toLowerCase().contains(charString) ||
androidVersion.getName().toLowerCase().contains(charString) ||
androidVersion.getVer().toLowerCase().contains(charString)) {
filteredList.add(androidVersion);
}
答案 1 :(得分:0)
您需要添加一些预处理以确定是否存在多个值
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String splitter = ",";
final String[] multipleValues = charSequence.toString().split(splitter);
//your null check
List<AndroidVersions> filteredList = new ArrayList()
for (String value : multipleValues) {
filteredList.addAll(internalFilter(mArrayList, value));
}
//the rest of your code
}
private List<AndroidVersions> internalFilter(ArrayList<AndroidVersions> listToFilter, String value) {
List<AndroidVersions> filteredList = new ArrayList()
for (AndroidVersion androidVersion : listToFilter) {
if (androidVersion.getApi().toLowerCase().contains(value)
|| androidVersion.getName().toLowerCase().contains(value)
|| androidVersion.getVer().toLowerCase().contains(value)) {
filteredList.add(androidVersion);
}
}
return filteredList;
}
此代码也可以完美地使用1个值