0

リストビューにインストールされているアプリケーションを除外するために、リストビューを検索ボックスと連携させようとしています。toString() メソッドのオーバーライドや getFilter() メソッドのオーバーライドなど、さまざまな方法を試しましたが、どれも機能していないようです。

主な活動:

public class AllApplicationsActivity extends Activity {
    private ListView mListAppInfo;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // set layout for the main screen
        setContentView(R.layout.layout_main);

        // load list application
        mListAppInfo = (ListView)findViewById(R.id.lvApps);
        EditText search = (EditText)findViewById(R.id.EditText01);

        mListAppInfo.setTextFilterEnabled(true);

        // create new adapter
        final AppInfoAdapter adapter = new AppInfoAdapter(this, Utilities.getInstalledApplication(this), getPackageManager());


        // set adapter to list view  
        mListAppInfo.setAdapter(adapter);


        search.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {

            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
            }

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

        // implement event when an item on list view is selected
        mListAppInfo.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView parent, View view, int pos, long id) {
                // get the list adapter
                AppInfoAdapter appInfoAdapter = (AppInfoAdapter)parent.getAdapter();
                // get selected item on the list
                ApplicationInfo appInfo = (ApplicationInfo)appInfoAdapter.getItem(pos);
                // launch the selected application
                //Utilities.launchApp(parent.getContext(), getPackageManager(), appInfo.packageName);
                Utilities.getPermissions(parent.getContext(), getPackageManager(), appInfo.packageName);
                //Toast.makeText(MainActivity.this, "You have clicked on package: " + appInfo.packageName, Toast.LENGTH_SHORT).show();
            }
        });


    }
}

AppInfoAdapter

public class AppInfoAdapter extends ArrayAdapter<ApplicationInfo> {

    private Context mContext;
    PackageManager mPackManager;

    public AppInfoAdapter(Context c, List<ApplicationInfo> list, PackageManager pm) {
        super(c, 0, list);
        mContext = c;
        mPackManager = pm;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // get the selected entry
        ApplicationInfo entry = (ApplicationInfo) getItem(position);

        Log.e("TAG", entry.toString());

        // 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 static List<ApplicationInfo> getInstalledApplication(Context context) {
    PackageManager packageManager = context.getPackageManager();

    List<ApplicationInfo> apps = packageManager.getInstalledApplications(0);
    Collections.sort(apps, new ApplicationInfo.DisplayNameComparator(packageManager));
    return apps;

}
4

1 に答える 1

1

あなたが行ったように TextWatcher を使用するとうまくいくはずです。リストにフォーカスがあるときに機能する独自のフィルターをリストが設定するため、setTextFilterEnabled を呼び出さないようにしてください。

私の推測では、 ApplicationInfo.toString() は、リストに表示されているもの以外のものを返していると思います。デフォルトの ArrayAdapter フィルタは各アイテムの getString() と一致するため、予期しないものに対してフィルタリングしている可能性があります。

これは、ラッパー オブジェクトを使用して toString() をオーバーライドするか、独自のフィルターを作成することで解決できます。

  @Override
  public Filter getFilter() {
    return mFilter;
  }

  private final Filter mFilter = new Filter() {
    @Override
    protected FilterResults performFiltering(CharSequence charSequence) {
      FilterResults results = new FilterResults();
      if (charSequence == null) {
        return results;
      }

      // snip

      results.values = /* snip */
      results.count = /* snip */
      return results;
    }

    @Override
    protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
      if (filterResults != null) {
        notifyDataSetChanged();
      } else {
        notifyDataSetInvalidated();
      }
    }
  };

少なくとも、独自のフィルターを提供すると、デバッグに役立つ場合があります。また、パッケージ名とラベルで正規表現検索を行うフィルターを提供することも想像できます。

于 2012-07-27T10:26:20.073 に答える