這是我的應用程序所做的,用戶可以通過搜索框進行搜索。當他鍵入內容並匹配時,它會過濾gridview。但是當他試圖刪除時,沒有記錄顯示。 清空搜索框時,我被困在顯示整個記錄。Android GridView不在空搜索框中顯示記錄
這是我已經試過:
MainActivity.class:
etSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
adapter.getFilter().filter(cs.toString());
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void afterTextChanged(Editable cs) {
}
});
CustomAdapter.class:
@Override
public Filter getFilter() {
if (starsFilter == null)
starsFilter = new StarsFilter();
return starsFilter;
}
private class StarsFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
// Create a FilterResults object
FilterResults results = new FilterResults();
constraint = constraint.toString().toLowerCase();
if (constraint != null && constraint.toString().length() > 0) {
ArrayList<HashMap<String, String>> filteredStars = new ArrayList<>();
for(HashMap<String, String> item : data){
if(item.toString().toLowerCase().contains(constraint)){
filteredStars.add(item);
}
}
results.values = filteredStars;
results.count = filteredStars.size();
} else {
synchronized(this) {
results.values = data;
results.count = data.size();
}
}
// Return our FilterResults object
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
data.clear();
if (constraint.length() == 0) {
notifyDataSetInvalidated();
} else {
for (HashMap<String, String> result : (ArrayList<HashMap<String, String>>) results.values) {
data.add(result);
}
notifyDataSetChanged();
}
}
}
任何想法?我會很感激你的幫助。提前致謝!
問題是什麼?這個問題並不清楚。 – Lingviston
你真的想要做什麼,你在哪裏阻止 – Sree
@Lingviston我已更新我的帖子 –