具有搜索功能的Android Multichoice Item对话框

时间:2013-04-07 23:08:13

标签: android android-dialog

在我的Android应用程序中,我有一个包含大量项目的多选项目对话框(类似于图像中显示的对话框)。因此,我计划添加一个搜索框,并作为用户类型,相应地过滤项目。做这个的最好方式是什么?感谢

Image

1 个答案:

答案 0 :(得分:0)

我是这样做的,对我来说很好。

private static final class ListItemWithIndex {
        public final int index;
        public final String value;

        public ListItemWithIndex(final int index, final String value) {
            super();
            this.index = index;
            this.value = value;
        }

        @Override
        public String toString() {
            return value;
        }
    }

    public static void showMultiChoiceDialogWithSearchFilterUI(final Activity _activity, final Object[] _optionsList,
            final int _titleResId, final OnClickListener _itemSelectionListener) {
        final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(_activity);

        final List<ListItemWithIndex> allItems = new ArrayList<ListItemWithIndex>();
        final List<ListItemWithIndex> filteredItems = new ArrayList<ListItemWithIndex>();

        for (int i = 0; i < _optionsList.length; i++) {
            final Object obj = _optionsList[i];
            final ListItemWithIndex listItemWithIndex = new ListItemWithIndex(i, obj.toString());
            allItems.add(listItemWithIndex);
            filteredItems.add(listItemWithIndex);
        }

        dialogBuilder.setTitle(_titleResId);
        final ArrayAdapter<ListItemWithIndex> objectsAdapter = new ArrayAdapter<ListItemWithIndex>(_activity,
                android.R.layout.simple_list_item_1, filteredItems) {
            @Override
            public Filter getFilter() {
                return new Filter() {
                    @SuppressWarnings("unchecked")
                    @Override
                    protected void publishResults(final CharSequence constraint, final FilterResults results) {
                        filteredItems.clear();
                        filteredItems.addAll((List<ListItemWithIndex>) results.values);
                        notifyDataSetChanged();
                    }

                    @Override
                    protected FilterResults performFiltering(final CharSequence constraint) {
                        final FilterResults results = new FilterResults();

                        final String filterString = constraint.toString();
                        final ArrayList<ListItemWithIndex> list = new ArrayList<ListItemWithIndex>();
                        for (final ListItemWithIndex obj : allItems) {
                            final String objStr = obj.toString();
                            if ("".equals(filterString)
                                    || objStr.toLowerCase(Locale.getDefault()).contains(
                                            filterString.toLowerCase(Locale.getDefault()))) {
                                list.add(obj);
                            }
                        }

                        results.values = list;
                        results.count = list.size();
                        return results;
                    }
                };
            }
        };

        final EditText searchEditText = new EditText(_activity);
        searchEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(final CharSequence arg0, final int arg1, final int arg2, final int arg3) {
            }

            @Override
            public void beforeTextChanged(final CharSequence arg0, final int arg1, final int arg2, final int arg3) {
            }

            @Override
            public void afterTextChanged(final Editable arg0) {
                objectsAdapter.getFilter().filter(searchEditText.getText());
            }
        });

        final ListView listView = new ListView(_activity);
        listView.setAdapter(objectsAdapter);
        final LinearLayout linearLayout = new LinearLayout(_activity);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.addView(searchEditText, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, 0);
        layoutParams.weight = 1;
        linearLayout.addView(listView, layoutParams);
        dialogBuilder.setView(linearLayout);

        final AlertDialog dialog = dialogBuilder.create();
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(final AdapterView<?> parent, final View view, final int position, final long id) {
                dialog.dismiss();
                _itemSelectionListener.onClick(null, filteredItems.get(position).index);
            }
        });
        dialog.show();
    }

只需使用“showMultiChoiceDialogWithSearchFilterUI”函数即可。 您可能希望从XML中扩展edittext和listview,因为在此解决方案中,视图创建是硬编码的。