0

リストビューの現在の実装では、表示されるオブジェクトの変数に応じて、さまざまなカテゴリ/セクションにデータが表示されます。たとえば、データセットが {cat,one,red,five,orange,dog} の場合、結果のリストビューは {animals: cat,dog}, {colors: red, orange}, {numbers: one} になります。 、 五}。このために、オンラインで見つけた「SectionerAdapter」のバリエーションを使用しています。この場合、各セクションにカスタム ArrayAdapter<> を使用しています。このコードで提供されるセクションは、Android デバイスの設定アプリのセクションと似ています。

今、私はそれらの結果をフィルタリングしようとしています。「O」と入力すると、リストは {animals: empty},{colors:orange},{numbers:one} になります。問題は、リスト全体では機能せず、セクションの 1 つだけで機能することです。そのため、リスト全体に別のアプローチを使用しようとしています: ExpandableListView.

ExpandableListView 内でのフィルタリングが可能/簡単かどうか知っている人はいますか? それを行う方法を理解するために使用できる例はありますか?

ありがとう!

4

2 に答える 2

1

私は自分のプロジェクトで同様のことをしました。私は家にいないので、残念ながらコード サンプル ハンドルを持っていません。これが私がこれを達成した方法です-

カスタム ArrayAdapter<T> を SectionArrayAdapter<SectionedListItem> のベースとして使用します。SectionedListItem は、SectionedList に表示するすべてのアイテムの基本クラスとして使用されます。SectionedListItem は、いくつかのプロパティを定義します。

boolean isNewSection;
String sectionLabel;

これはインターフェースでもかまいませんが、クラスである必要はありません。それをクラスとして持つことは、私の実装にとって理にかなっています。

次に、セクション化されたリストに表示するアイテムのリストを取得し、それらをアダプターに適用する前に、カスタムの並べ替えを行います。リストを並べ替えるときに、isNewSection プロパティを true に設定して、新しいセクションを開始するインデックスに空の SectionedListItems を追加します。SectionedArrayAdapter がレンダリングを行うとき、isNewSection プロパティが true かどうかを確認します。true の場合、デフォルトのリスト項目の代わりにセクション ヘッダーをレンダリングします。

これにより、さまざまなリストの束ではなく、フィルタリング中に操作する単一のリストが得られます。ただし、独自の課題があります-フィルタリング後にリストを再ソートする必要があるか、フィルタリング中に新しいセクションを定義するためにのみ使用される SectionedListItems を無視する必要があります。

これが最善のアプローチだと主張しているわけではありません。これは私が思いついたアプローチです:)

于 2010-12-16T15:09:52.533 に答える
0

このコードはプロトタイプの一部として書かれているので、あまりきれいでも滑らかでもないことに注意してください:) しかし、正しい方向に進むはずです。また、InventoryListItem は、上記のプロパティを含む SectionedListItem クラスを拡張することにも注意してください。

/* -------------------------
 *      Class: InventoryAdapter 
 * ------------------------- */
private final class InventoryAdapter extends ArrayAdapter<InventoryListItem> implements Filterable {
        /* -------------------------
         *      Fields 
         * ------------------------- */
        private ArrayList<InventoryListItem> items;
        private ArrayList<InventoryListItem> staticItems;
        private int resource;
        private InventoryFilter filter = null;
        /* -------------------------
         *      Constructor 
         * ------------------------- */
        public InventoryAdapter(Context context, int textViewResourceId, ArrayList<InventoryListItem> objects) {
                super(context, textViewResourceId, objects);
                items = objects;
                resource = textViewResourceId;
                staticItems = items;
        }

        /* -------------------------
         *      Private Methods 
         * ------------------------- */
        private void addCategorySpan(SpannableString span, int startIndex, int endIndex, final String category) {
                span.setSpan(new ClickableSpan() {
                        @Override
                        public void onClick(View widget) {
                                String categoryFilter = "cat://" + category;
                                filterList(categoryFilter, true);
                        }
                }, startIndex, endIndex, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

        /* -------------------------
         *      Public Methods 
         * ------------------------- */
        // Properties
        @Override
        public int getCount() {
                return items.size();
        }
        @Override
        public InventoryListItem getItem(int position) {
                return items.get(position);
        }
        @Override
        public int getPosition(InventoryListItem item) {
                return items.indexOf(item);
        }
        @Override
        public long getItemId(int position) {
                return items.get(position).id;
        }
        @Override
        public boolean isEnabled(int position) {
                return true;
        }
        // Methods
        public Filter getFilter() {
                if ( filter == null ) {
                        filter = new InventoryFilter();
                }
                return filter;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View v = null;

                InventoryListItem item = items.get(position);
                if ( item.startNewSection ) {
                        v = inflater.inflate(R.layout.sectioned_list_header, null);

                        TextView sectionText = (TextView)v.findViewById(R.id.list_header_title);
                        sectionText.setText(item.sectionName);
                } else {
                        v = inflater.inflate(resource, null);
                        TextView nameText = (TextView)v.findViewById(R.id.inventory_list_item_name_text);
                        TextView qtyText = (TextView)v.findViewById(R.id.inventory_list_item_qty_text);
                        TextView brandText = (TextView)v.findViewById(R.id.inventory_list_item_brand_text);
                        final TextView categoryText = (TextView)v.findViewById(R.id.inventory_list_item_category_text);

                        nameText.setText(item.name);
                        String qty = Float.toString(item.remainingAmount) + " " + item.measurementAbbv;
                        qtyText.setText(qty);
                        brandText.setText(item.brand);

                        // Create our list of categories and patterns
                        String categories = "";
                        for ( int i = 0; i <= item.categories.size() - 1; i++ ) {
                                if ( categories.length() == 0 ) {
                                        categories = item.categories.get(i);
                                } else {
                                        categories += ", " + item.categories.get(i);
                                }
                        }
                        categoryText.setMovementMethod(LinkMovementMethod.getInstance());
                        categoryText.setText(categories, BufferType.SPANNABLE);
                        // Now creat our spannable text
                        SpannableString span = (SpannableString)categoryText.getText();
                        // Create our links and set our text
                        int startIndex = 0;
                        boolean stillLooking = true;
                        while ( stillLooking ) {
                                int commaIndex = categories.indexOf(", ", startIndex);
                                if ( commaIndex >= 0 ) {
                                        final String spanText = categoryText.getText().toString().substring(startIndex, commaIndex);
                                        addCategorySpan(span, startIndex, commaIndex, spanText);
                                        startIndex = commaIndex + 2;
                                } else {
                                        final String spanText = categoryText.getText().toString().substring(startIndex, categoryText.getText().toString().length());
                                        addCategorySpan(span, startIndex, categoryText.getText().toString().length(), spanText);
                                        stillLooking = false;
                                }
                        }
                        v.setTag(item.id);
                }

                return v;
        }

        /* -------------------------
         *      Class: InventoryFilter 
         * ------------------------- */
        private class InventoryFilter extends Filter {
                private Object lock = new Object();

                /* -------------------------
                 *      Protected Methods
                 * ------------------------- */
                @Override
                protected FilterResults performFiltering(CharSequence constraint) {
                        FilterResults results = new FilterResults();
                        if ( constraint == null || constraint.length() == 0 ) {
                                synchronized(lock) {
                                        items = staticItems;
                                        results.values = items;
                                        results.count = items.size();
                                }
                        } else {
                                String searchString = constraint.toString();
                                // Do our category search
                                if ( searchString.startsWith("cat:") ) {
                                        String trimmedSearch = searchString.substring(searchString.indexOf("://") + 3);
                                        // Do our search
                                        ArrayList<InventoryListItem> newItems = new ArrayList<InventoryListItem>();
                                        for ( int i = 0; i <= items.size() - 1; i++ ) {
                                                InventoryListItem item = items.get(i);
                                                // See if we're a section, and if we have an item under us
                                                if ( item.startNewSection && i < items.size() - 1 ) {
                                                        InventoryListItem next = items.get(i + 1);
                                                        if ( next.sectionName == item.sectionName ) {
                                                                if ( !next.startNewSection && next.categories.contains(trimmedSearch) ) {
                                                                        newItems.add(item);
                                                                }
                                                        }
                                                }
                                                else if ( !item.startNewSection && item.categories.contains(trimmedSearch) ) {
                                                        newItems.add(item);
                                                }
                                        }

                                        results.values = newItems;
                                        results.count = newItems.size();
                                }
                        }
                        return results;
                }
                @SuppressWarnings("unchecked")
                @Override
                protected void publishResults(CharSequence constraint, FilterResults results) {
                        //noinspection unchecked
                        items = (ArrayList<InventoryListItem>)results.values;
                        // Let the adapter know about the updated list
                        if (results.count > 0) {
                                notifyDataSetChanged();
                        } else {
                                notifyDataSetInvalidated();
                            }
                }
        }
}
于 2010-12-17T13:16:00.520 に答える