How to implement getFilter on a BaseAdapter?

前端 未结 7 843
半阙折子戏
半阙折子戏 2020-12-06 03:55

I am trying to implement a getFilter() on a base adapter to filter out search results on a List. Is there any example of how to implement a getFilter()?

MainActivity

7条回答
  •  甜味超标
    2020-12-06 04:21

    Can you post your full AppInfoAdapter? Also is there any reason extending from BaseAdapter and not ArrayAdapter? If you have an ArrayList of objects, use ArrayAdapter, it already implements Filterable interface.

    Actually you are using a List, your adapter can be rewritten to extends ArrayAdapter which already is Filterable.

    public class AppInfoAdapter extends ArrayAdapter {
    
        private Context mContext;
        PackageManager mPackManager;
    
        public AppInfoAdapter(Context c, List list, PackageManager pm) {
            super(c, 0, new ArrayList());
            mContext = c;
            mPackManager = pm;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // get the selected entry
            ApplicationInfo entry = (ApplicationInfo) getItem(position);
    
            // reference to convertView
            View v = convertView;
    
            // inflate new layout if null
            if(v == null) {
                LayoutInflater inflater = LayoutInflater.from(mContext);
                v = inflater.inflate(R.layout.layout_appinfo, null);
            }
    
            // load controls from layout resources
            ImageView ivAppIcon = (ImageView)v.findViewById(R.id.ivIcon);
            TextView tvAppName = (TextView)v.findViewById(R.id.tvName);
            TextView tvPkgName = (TextView)v.findViewById(R.id.tvPack);
    
            // set data to display
            ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
            tvAppName.setText(entry.loadLabel(mPackManager));
            tvPkgName.setText(entry.packageName);
    
            // return view
            return v;
        }
    }
    

提交回复
热议问题