如何过滤android自定义列表视图?

时间:2014-04-04 15:36:36

标签: android listview filter

我有自定义 ListView ,我想过滤它。它有4或5列,并希望在一列上过滤它以显示搜索过滤器的所有结果 我用的时候:

((Filterable) FicheClient1.this.adapter).getFilter().filter(cs);

它显示我加倍并在所有行中过滤。

我的 ListView

        ArrayList<HashMap<String, String>> clientList =  controller.getAllClient();
    if(clientList.size()!=0) {
        ListView lv = getListView();
        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override 
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
                clientId = (TextView) view.findViewById(R.id.clientId);
                nom = (TextView) view.findViewById(R.id.name);
                emailtext = (TextView) view.findViewById(R.id.emailficheclient);
                String valclientId = clientId.getText().toString(); 
                String nameString = nom.getText().toString();
                String emailString = emailtext.getText().toString();
                cli.setNamelist(nameString);
                cli.setClientId(valclientId);
                cli.setEmailString(emailtext.getText().toString());
                Intent  objIndent = new Intent(getApplicationContext(),EditNouveauClient.class);
                objIndent.putExtra("clientId", valclientId);
                objIndent.putExtra("nom", nameString);
                startActivity(objIndent);
                FicheClient1.this.finish();
            }
        });
        adapter = new SimpleAdapter( FicheClient1.this,clientList, R.layout.client_item_row, new String[] { "clientId","civilite"
                ,"name","prenom","naissance","email"}, 
                new int[] {R.id.clientId, R.id.civilite, R.id.name, R.id.prenom, R.id.naissance, R.id.emailficheclient}); 
        setListAdapter(adapter);
        lv.setTextFilterEnabled(true);

SearchFilter

inputSearch.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3
        ((Filterable) FicheClient1.this.adapter).getFilter().filter(cs);
    }
});

2 个答案:

答案 0 :(得分:0)

对于过滤器,假设您的edittext名称为searchbar,并且适配器名称为adpater

    serchbar.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text

            adpater.getFilter().filter(cs.toString());
         }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub                          
        }

    });

adpater中的过滤器将是..

/*** filter text data ***/


    public Filter getFilter() {
    Filter filter = new Filter() {

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,FilterResults results) {

            menuItems = (   ArrayList<HashMap<String, String>>) results.values; // has the filtered values
            notifyDataSetChanged();  // notifies the data with new filtered values
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();        // Holds the results of a filtering operation in values
            ArrayList<HashMap<String, String>> FilteredArrList = new ArrayList<HashMap<String, String>>();

            if (mDisplayedValues == null) {
                mDisplayedValues = new ArrayList<HashMap<String, String>>(menuItems); // saves the original data in mOriginalValues
            }

            /********
             * 
             *  If constraint(CharSequence that is received) is null returns the mOriginalValues(Original) values
             *  else does the Filtering and returns FilteredArrList(Filtered)  
             *
             ********/
            if (constraint == null || constraint.length() == 0) {

                // set the Original result to return  
                results.count = mDisplayedValues.size();
                results.values = mDisplayedValues;
            } else {
                constraint = constraint.toString().toLowerCase();
                for (int i = 0; i < mDisplayedValues.size(); i++) {
                    String data =      mDisplayedValues.get(i).get(ServerConstant.key_event_name);
                    Log.v("publish result", data);
                    if (data.toLowerCase().startsWith(constraint.toString())) {

                        Log.i("publish result match",constraint.toString() +"hiiiii " + data);
                        FilteredArrList.add(mDisplayedValues.get(i));
                    }
                }
                // set the Filtered result to return
                results.count = FilteredArrList.size();
                results.values = FilteredArrList;
            }
            return results;
        }
    };
    return filter;
}

答案 1 :(得分:0)

由于您HashMapListView,因此您需要Filterable适配器的工具ListView界面并覆盖此getFilter()您需要编写用于过滤列表的逻辑的方法。

例如:

public Filter getFilter() {
    Filter filter = new Filter() {

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,FilterResults results) {

            menuItems = (   ArrayList<HashMap<String, String>>) results.values; // has the filtered values
            notifyDataSetChanged();  // notifies the data with new filtered values
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();        // Holds the results of a filtering operation in values
            ArrayList<HashMap<String, String>> FilteredArrList = new ArrayList<HashMap<String, String>>();

            if (mDisplayedValues == null) {
                mDisplayedValues = new ArrayList<HashMap<String, String>>(menuItems); // saves the original data in mOriginalValues
            }

            /********
             * 
             *  If constraint(CharSequence that is received) is null returns the mOriginalValues(Original) values
             *  else does the Filtering and returns FilteredArrList(Filtered)  
             *
             ********/
            if (constraint == null || constraint.length() == 0) {

                // set the Original result to return  
                results.count = mDisplayedValues.size();
                results.values = mDisplayedValues;
            } else {
                constraint = constraint.toString().toLowerCase();
                for (int i = 0; i < mDisplayedValues.size(); i++) {
                    String data =      mDisplayedValues.get(i).get(ServerConstant.key_event_name);
                    Log.v("publish result", data);
                    if (data.toLowerCase().startsWith(constraint.toString())) {

                        Log.i("publish result match",constraint.toString() +"hiiiii " + data);
                        FilteredArrList.add(mDisplayedValues.get(i));
                    }
                }
                // set the Filtered result to return
                results.count = FilteredArrList.size();
                results.values = FilteredArrList;
            }
            return results;
        }
    };
    return filter;
}

了解更多详情Click here