3

リストの検索結果を除外するために、ベース アダプターに getFilter() を実装しようとしています。getFilter() を実装する方法の例はありますか?

MainActivity.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 を追加しました

4

7 に答える 7

7

アダプターで、このクラスを 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;
    }

完全な例

于 2013-01-16T13:23:48.437 に答える
2

完全に投稿できますAppInfoAdapterか?BaseAdapterまた、 からではなくから伸びる理由はありますArrayAdapterか?ArrayListオブジェクトの を使用する場合はArrayAdapter、既にインターフェイスを実装していFilterableます。

実際には を使用していますがList、アダプターは extends に書き換えることができますが、ArrayAdapterこれは既にFilterable.

public class AppInfoAdapter extends ArrayAdapter<ApplicationInfo> {

    private Context mContext;
    PackageManager mPackManager;

    public AppInfoAdapter(Context c, List<ApplicationInfo> list, PackageManager pm) {
        super(c, 0, new ArrayList<ApplicationInfo>());
        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;
    }
}
于 2012-07-23T20:30:46.867 に答える
1

のインスタンスを返す必要がありますFilter。フィルタを作成するには、サブクラス化Filterして and を実装performFilteringpublishResultsます。ドキュメントを参照してください。

于 2012-07-23T20:26:29.583 に答える
0

getFilter() はアダプタでオーバーライドして、filtered list を含むフィルタ オブジェクトを返すことができます。Filter() クラスには 2 つの主要なメソッドがあります。performFilteringpublishResults。最初のメソッドはワーカー スレッドでフィルタリングを実行し、後のメソッドはフィルタリングされたオブジェクトのリストを返します。

以下のサンプルコードを参照できます

@Override
public Filter getFilter() {

        return new Filter() {

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                // TODO Auto-generated method stub
                if (results.count == 0) {
                    notifyDataSetInvalidated();
                }else{
                    mListAppInfo = (ArrayList<SampleItem>) results.values;
                    notifyDataSetChanged();
                }
            }

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

                if (constraint == null || constraint.length() == 0) {
                    results.values = mListAppInfo;
                    results.count = mListAppInfo.size();
                }else{
                    ArrayList<SampleItem> filter_items = new ArrayList<>(); 
                    for (SampleItem item : mListAppInfo) {
                        if (item.getItemName().toLowerCase().startsWith(constraint.toString().toLowerCase())) {
                            filter_items.add(item);
                        }
                    }
                    results.values =  filter_items ;
                    results.count = filter_items.size();
                }
                return results;
            }
        };
    }

お役に立てば幸いです。

于 2014-09-07T08:11:43.330 に答える
-1

クラスをArrayAdapterで拡張し、メソッドをオーバーライドし、フィルタークラスのオブジェクトを作成して返します。

于 2014-09-26T04:23:40.917 に答える