2

我有一个如下所示的适配器,我使用 getFilter 方法来过滤列表视图中的数据。它有效,但过滤后,只有过滤后的数据。我的意思是,例如,当我过滤使用“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;    
    }
4

1 回答 1

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();
于 2013-06-27T12:25:55.527 回答