0

私のアプリには、ユーザーがインストールしたアプリケーションのリストがあり、そのリストの検索機能を作成したいと考えています。今、ここに私のコーディングがあります:

       // create new adapter
AppInfoAdapter adapter = new AppInfoAdapter(this, Utilities.getInstalledApplication(this),   getPackageManager());
// load list application
mListAppInfo = (ListView)findViewById(R.id.lvApps);
// set adapter to list view
mListAppInfo.setAdapter(adapter);
// search bar
inputSearch = (EditText) findViewById(R.id.inputSearch);

inputSearch.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {

        // When user changed the Text
        // Drag_and_Drop_App.this.adapter.getFilter().filter(cs);  
        Drag_and_Drop_App.this.adapter.getFilter().filter(cs);

    }

    @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                          
    }
    });

この問題は、次の行でエラーが発生したときに発生します。

Drag_and_Drop_App.this.adapter.getFilter().filter(cs);

これは、「getFilter()」が私の基本アダプターで定義されていないことを示しています。

package com.example.awesomefilebuilderwidget;

IMPORTS

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

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

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

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

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

@Override
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;
}

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

stackoverflow を見て回った最後の部分「public Filter...」を追加しました。しかし今、検索用のカスタム フィルターが必要です。何を使用できますか?(私はすでに1つのことを試しましたが、うまくいきません)

4

1 に答える 1

0

getFilter()アダプターは Filterable インターフェースを実装する必要があるため、メソッドは定義されていません。アダプターに「implements Filterable」を追加するだけです。

public class AppInfoAdapter extends BaseAdapter implements Filterable {
    // your stuff
}

そしてあなたのgetFilterで

@Override
public Filter getFilter() {
    if(filter == null) {
        filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();
                List<ApplicationInfo> myFilteredAppList = new ArrayList<ApplicationInfo>();
                constraint = constraint.toString().toLowerCase();

                for (ApplicationInfo appInfo : originalListAppInfo) {
                    String somefield = appInfo.getSomeField();
                    if (somefield.toLowerCase().contains(constraint.toString())) {
                        myFilteredAppList.add(appInfo);
                    }
                }
                results.count = myFilteredAppList.size();
                results.values = myFilteredAppList;
                return results;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                mListAppInfo = (List<ApplicationInfo>)results.values;
                notifyDataSetChanged();
            }
        };
    }
    return filter;
}

アダプターの作成中に作成された元のリスト (originalListAppInfo) のコピーを反復処理したことに注意してください。

private List<ApplicationInfo> originalListAppInfo;
private List<ApplicationInfo> mListAppInfo;

private Filter filter;

public AppInfoAdapter(Context context, List<ApplicationInfo> listApp) {
    this.context = context;
    this.originalListAppInfo = this.mListAppInfo = listApp;
}

この助けを願っています。:)

于 2013-10-17T23:09:02.743 に答える