android - 如何在 BaseAdapter 上实现 getFilter?

标签 android listview filter baseadapter

我正在尝试在基本适配器上实现 getFilter() 以过滤掉列表中的搜索结果。有没有关于如何实现 getFilter() 的示例?

主 Activity .java

   final AppInfoAdapter adapter = new AppInfoAdapter(this, Utilities.getSystemFilteredApplication(this), getPackageManager());


        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
           adapter.getFilter().filter(s); //Filter from my adapter
           adapter.notifyDataSetChanged(); //Update my view
        }

AppInfoAdapter.java

package com.example.permission;

import java.util.List;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

public class AppInfoAdapter extends BaseAdapter implements Filterable{
    private Context mContext;
    private List mListAppInfo;
    PackageManager mPackManager;

    public AppInfoAdapter(Context c, List list, PackageManager pm) {
        mContext = c;
        mListAppInfo = list;
        mPackManager = pm;
    }

    public int getCount() {
        return mListAppInfo.size();
    }


    public Object getItem(int position) {
        return mListAppInfo.get(position);
    }


    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        // get the selected entry
        ApplicationInfo entry = (ApplicationInfo) mListAppInfo.get(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;
    }

    public Filter getFilter() {
        // TODO Auto-generated method stub
        return null;
    }


}

编辑:编辑代码并添加完整的 AppInfoAdapter.java

最佳答案

在您的适配器中放入此类以在 getfilter 方法中使用它

//this is a simple class that filtering the ArrayList of strings used in adapter

public class filter_here extends Filter{

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            // TODO Auto-generated method stub

            FilterResults Result = new FilterResults();
            // if constraint is empty return the original names
            if(constraint.length() == 0 ){
                Result.values = Original_Names;
                Result.count = Original_Names.size();
                return Result;
            }

            ArrayList<String> Filtered_Names = new ArrayList<String>();
            String filterString = constraint.toString().toLowerCase();
            String filterableString;

            for(int i = 0; i<Original_Names.size(); i++){
                filterableString = Original_Names.get(i);
                if(filterableString.toLowerCase().contains(filterString)){
                    Filtered_Names.add(filterableString);
                }
            }
            Result.values = Filtered_Names;
            Result.count = Filtered_Names.size();

            return Result;
        }

        @Override
        protected void publishResults(CharSequence constraint,FilterResults results) {
            // TODO Auto-generated method stub
            Names = (ArrayList<String>) results.values;
            notifyDataSetChanged();
        }

    }

在 getfilter 中从它返回实例

@Override
    public Filter getFilter() {
        // TODO Auto-generated method stub
        return filter;
    }

full example

关于android - 如何在 BaseAdapter 上实现 getFilter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11619874/

相关文章:

android - Android 应用本地化

android - 在gradle中根据Android buildType切换Application.mk文件

java - 如何按字母顺序返回已安装应用程序的列表

android - 如何从手机中获取所有联系人并在 android 中单击按钮时显示在 ListView 中?

Grails:如何在过滤器中使用createLink?

android - 我们如何在 Android 1.5 的 OpenGL ES 中获得多边形抗锯齿?

android - 谷歌播放控制台说 "You can' t 编辑这个应用程序,直到你创建一个声明敏感权限的新应用程序版本“如何修复它?

ios - ListView 到uiwebview?

audio - 为 Alsa/Pulse 环回创建实时音频过滤器

JavaNNS - 解析创建的神经网络