如何为动态填充的适配器保留原始数据,需要在用户对列表视图进行搜索请求之前对数据进行快照> 我正在myadapter中实现Filterable接口,并且无法在构造函数上复制原始数据,因为我的列表已经为空,以后将被填充。
private Filter filteredList = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
List<ITEMMODEL> filtered = new ArrayList<>();
if (charSequence == null || charSequence.length() == 0) {
filtered.addAll(itemsFull);
} else {
for (ITEMMODEL item : itemsFull) {
Log.d(TAG,item.getName());
if (item.getName().toLowerCase().contains(charSequence.toString().toLowerCase().trim())) {
filtered.add(item);
}
}
}
FilterResults results = new FilterResults();
results.values = filtered;
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
items.clear();
items.addAll((List)filterResults.values);
notifyDataSetChanged();
}
};
public void onResume() {
super.onResume();
Cursor cursor = getActivity().getContentResolver().query(ItemsProvider.CONTENT_URI,null,null,null,null);
itemListAdapter.clear();
while ((cursor.moveToNext())) {
itemListAdapter.addItem(itemFromCursor(cursor));
}
}
答案 0 :(得分:0)
尝试在适配器内部,修改如下的 clear()方法:
@Override
public void clear() {
super.clear();
itemsFull.clear();
}
和 addItem()方法可将项目添加到两个列表(items和itemsFull)。希望有帮助!