2017-06-02 447 views
0

目前我只能搜索一個字符串單詞。例如,我需要搜索多個詞,我鍵入「牛軋糖,甜甜圈」,他們告訴我這兩個?我需要在代碼中使用這個工作嗎?Recycler查看多個搜索

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); 

    } 
} 

json recycler view

this is search but only for one word

+0

你可以從服務器端編程做在API中,你從數據庫中取回數據。根據你的過濾詞做搜索sql –

回答

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); 
    } 
+0

這工作得很好,但是當我寫3個屬於同一張卡的單詞時,我得到的卡顯示3次 –

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個值完全正常