Filter ListView with arrayadapter

前端 未结 2 565
一向
一向 2020-11-28 12:25

I\'m trying to filter a listview with arrayadapter. The arraydapter parameter is a String[][]. The problem is that anything happens. I must override the Filter interface? In

相关标签:
2条回答
  • 2020-11-28 12:31

    Convert your String array to ArrayList and pass it to Adapter and use below code or change below code with your String[].

    You need to implement Filterable to your Adapter class and Override getFilter()

    Checkout this complete example for filtering custom Adapter.

    public class ListFilterActivity extends ListActivity {
    
        private List<String> list = new ArrayList<String>();
        List<String> mOriginalValues;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            final MyAdapter adapter = new MyAdapter(this, getModel());
            setListAdapter(adapter);
    
            EditText filterEditText = (EditText) findViewById(R.id.filterText);
    
            // Add Text Change Listener to EditText
            filterEditText.addTextChangedListener(new TextWatcher() {
    
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    // Call back the Adapter with current character to Filter
                    adapter.getFilter().filter(s.toString());
                }
    
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,int after) {
                }
    
                @Override
                public void afterTextChanged(Editable s) {
                }
            });
        }
    
        private List<String> getModel() {
            list.add("Linux");
            list.add("Windows7");
            list.add("Suse");
            list.add("Eclipse");
            list.add("Ubuntu");
            list.add("Solaris");
            list.add("Android");
            list.add("iPhone");
            list.add("Windows XP");
            return list;
        }
    }
    
    // Adapter Class
    public class MyAdapter extends BaseAdapter implements Filterable {
    
        List<String> arrayList;
        List<String> mOriginalValues; // Original Values
        LayoutInflater inflater;
    
        public MyAdapter(Context context, List<String> arrayList) {
            this.arrayList = arrayList;
            inflater = LayoutInflater.from(context);
        }
    
        @Override
        public int getCount() {
            return arrayList.size();
        }
    
        @Override
        public Object getItem(int position) {
            return position;
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        private class ViewHolder {
            TextView textView;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
            ViewHolder holder = null;
    
            if (convertView == null) {
    
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.row, null);
                holder.textView = (TextView) convertView
                .findViewById(R.id.textview);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.textView.setText(arrayList.get(position));
            return convertView;
        }
    
        @Override
        public Filter getFilter() {
            Filter filter = new Filter() {
    
                @SuppressWarnings("unchecked")
                @Override
                protected void publishResults(CharSequence constraint,FilterResults results) {
    
                    arrayList = (List<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
                    List<String> FilteredArrList = new ArrayList<String>();
    
                    if (mOriginalValues == null) {
                        mOriginalValues = new ArrayList<String>(arrayList); // 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 = mOriginalValues.size();
                        results.values = mOriginalValues;
                    } else {
                        constraint = constraint.toString().toLowerCase();
                        for (int i = 0; i < mOriginalValues.size(); i++) {
                            String data = mOriginalValues.get(i);
                            if (data.toLowerCase().startsWith(constraint.toString())) {
                                FilteredArrList.add(data);
                            }
                        }
                        // set the Filtered result to return
                        results.count = FilteredArrList.size();
                        results.values = FilteredArrList;
                    }
                    return results;
                }
            };
            return filter;
        }
    }
    
    0 讨论(0)
  • 2020-11-28 12:33

    Here I mention the code of how We can filter the listView Data using custom Adapter
    Its really help you

    ListViewAdapter.java

    public class ListViewAdapter extends BaseAdapter {
    
        // Declare Variables
        Context mContext;
        LayoutInflater inflater;
        public List<AllFoodItem> allFoodItemlist;
        public ArrayList<AllFoodItem> arraylist;
    
        public ListViewAdapter(Context context, List<AllFoodItem> allFoodItemlist) {
            mContext = context;
            this.allFoodItemlist = allFoodItemlist;
            inflater = LayoutInflater.from(mContext);
            this.arraylist = new ArrayList<AllFoodItem>();
            this.arraylist.addAll(allFoodItemlist);
        }
        @Override
        public int getCount() {
            return allFoodItemlist.size();
        }
    
        @Override
        public AllFoodItem getItem(int position) {
            return allFoodItemlist.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        public class Holder {
            TextView foodname, quantity, calorie;    
        }    
        public View getView(final int position, View view, ViewGroup parent) {
            Holder holder = new Holder();
            view = inflater.inflate(R.layout.program_list, null);
            // Locate the TextViews in listview_item.xml
            holder.foodname = (TextView) view.findViewById(R.id.textView1);
             // Set the results into TextViews            holder.foodname.setText(allFoodItemlist.get(position).getName().toString());    
            return view;
        }
    
        // Filter Class
        public void filter(String charText) {
            charText = charText.toLowerCase(Locale.getDefault());
            allFoodItemlist.clear();
            if (charText.length() == 0) {
                allFoodItemlist.addAll(arraylist);
            } else {
                for (AllFoodItem wp : arraylist) {
                    if (wp.getName().toLowerCase(Locale.getDefault()).contains(charText)) {
                        allFoodItemlist.add(wp);
                    }
                }
            }
            notifyDataSetChanged();
        }
    
    }
    

    MainActivity.java

    use the ListViewAdapter class apply filter on EditText by using filter() method

    inputSearch.addTextChangedListener(new TextWatcher() {
    
                @Override
                public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                    // When user changed the Text
                    foodname = (inputSearch.getText().toString()).trim();
    
                    // filter the data of edit Text using filter method in ListViewAdapter
                    String text = inputSearch.getText().toString().toLowerCase(Locale.getDefault());
                    adapter.filter(text);
                }
    
                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                              int arg3) {
                    // TODO Auto-generated method stub
    
                }
    
                @Override
                public void afterTextChanged(Editable arg0) {
    
                    userGet();
                    // TODO Auto-generated method stub
                }
            });
    
    0 讨论(0)
提交回复
热议问题