我有一个像下面这样的适配器,我使用getFilter方法来过滤listview中的数据。它工作但过滤后,只有过滤数据。我的意思是例如当我过滤usind“test1”关键字时,有test1,test12,test13 ...,当我删除最后一个单词“test” 我希望过滤器再次加载整个数据。
我怎样才能解决这个问题。
search.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
listAdapter.getFilter().filter(s);
Log.v("textchanged",s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
private class BranchListAdapter extends ArrayAdapter<BranchViewModel>{
private Context context;
private ArrayList<BranchViewModel> itemList;
private int layoutResources;
public BranchListAdapter(Context context, int textViewResourceId,ArrayList<BranchViewModel> objects) {
super(context, textViewResourceId, objects);
this.context = context;
this.itemList = objects;
this.layoutResources = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = LayoutInflater.from(this.context).inflate(this.layoutResources, null);
}
ATextView aTextView =(ATextView) convertView.findViewById(R.id.havalePaymentTypeItemText);
aTextView.setText(itemList.get(position).getBranchCode() +"-"+ itemList.get(position).getBranchName());
return convertView;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,FilterResults results) {
Log.println(Log.INFO, "Results", "FOUND");
itemList.clear();
itemList.addAll((ArrayList<BranchViewModel>) results.values);
Log.e("VALUES","in publish : "+ String.valueOf(itemList.size()));
notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
ArrayList<BranchViewModel> FilteredBranchViewModel = new ArrayList<BranchViewModel>();
constraint = constraint.toString().toLowerCase();
for (int i = 0; i < itemList.size(); i++) {
String dataNames = itemList.get(i).getBranchName();
String dataId = itemList.get(i).getBranchCode();
if (dataNames.toLowerCase().startsWith(constraint.toString()) | dataId.toLowerCase().startsWith(constraint.toString())) {
FilteredBranchViewModel.add(itemList.get(i));
}
}
results.count = FilteredBranchViewModel.size();
results.values = FilteredBranchViewModel;
Log.e("VALUES","in filter : "+ String.valueOf(results.count));
return results;
}
};
return filter;
}
答案 0 :(得分:1)
查看ArrayAdapter.ArrayFilter
的来源。您会看到他们在performFiltering(CharSequence)
中复制了原始数据。
段:
if (mOriginalValues == null) {
synchronized (mLock) {
mOriginalValues = new ArrayList<T>(mObjects);
}
}
if (prefix == null || prefix.length() == 0) {
ArrayList<T> list;
synchronized (mLock) {
list = new ArrayList<T>(mOriginalValues);
}
}
results.values = list;
results.count = list.size();