核心邏輯方法:數組
/** * 搜索item * @param searchContent 須要搜索的文本內容 */ public void searchItem(String searchContent){ this.mSearchContent = searchContent.trim();//去除空格 if(TextUtils.isEmpty(mSearchContent)||mSearchContent.length()==0){//若是搜索內容是空的就顯示所有內容 this.mShowList.clear(); mShowList.addAll(mAllDataList); notifyDataSetChanged(); return; } List<THealthDataListBase.StudentData> tempList = new ArrayList<>();//用於臨時保存匹配完成的數據 THealthDataListBase.StudentData tempStudentData = null;//用於臨時保存匹配完成的item數據 char[] searchContentCharArray = searchContent.toCharArray(); for (THealthDataListBase.StudentData studentData : mAllDataList){ //遍歷所有學生名稱 char[] studentNameCharArray = studentData.name.trim().toCharArray();//學生名稱去除空格,而且轉成數組 int count = 0; for (int i=0;i<searchContentCharArray.length;i++){ //遍歷搜索文字 for (char word :studentNameCharArray){ //遍歷學生名稱文字 if (word == searchContentCharArray[i]){ //判斷一致的文字 count++; } } } if (count == 0){ //若是匹配度是0就不添加 continue; } tempStudentData = new THealthDataListBase().new StudentData(); tempStudentData.name = studentData.name; tempStudentData.studentId = studentData.studentId; tempStudentData.morning = studentData.morning; tempStudentData.noon = studentData.noon; tempStudentData.setCount(count); tempList.add(tempStudentData); } if (tempList.isEmpty() && !mSearchContent.isEmpty()){ mShowList.clear(); notifyDataSetChanged(); return; } Collections.sort(tempList);//排序 mShowList.clear(); mShowList.addAll(tempList); notifyDataSetChanged(); }