簡體   English   中英

如何將過濾器應用於customlistview

[英]How to apply filter to customlistview

我正在為列表視圖使用自定義適配器,並且我想應用過濾器以從列表中搜索特定項目該怎么做? 我知道如何對擴展ArrayAdapter的適配器執行此操作,但在我的情況下,它是simplesectionadapter。我在要查看該項目的edittext中輸入的文本的列表視圖上方有一個editText。

Collections.sort(contents, new Comparator<CategoryPojo>() {
            @Override
            public int compare(CategoryPojo s1, CategoryPojo s2) {
                return s1.getCategoryName().compareToIgnoreCase(
                        s2.getCategoryName());
            }
        });

        final CategoryAdapter adapter = new CategoryAdapter(this,
                android.R.layout.simple_list_item_1, contents);

        Sectionizer<CategoryPojo> alphabetSectionizer = new Sectionizer<CategoryPojo>() {

            @Override
            public String getSectionTitleForItem(CategoryPojo instance) {
                return instance.getCategoryName().substring(0, 1);
            }
        };

        final SimpleSectionAdapter<CategoryPojo> sectionAdapter = new SimpleSectionAdapter<CategoryPojo>(
                this, adapter, R.layout.section_header, R.id.title,
                alphabetSectionizer);

        listView.setFastScrollEnabled(true);
        listView.setTextFilterEnabled(true);
        listView.setAdapter(sectionAdapter);

這是簡單的節適配器

public class SimpleSectionAdapter<T> extends BaseAdapter implements Filterable{
    static final boolean DEBUG = false;
    static final String TAG = SimpleSectionAdapter.class.getSimpleName();

    // Constants
    private static final int VIEW_TYPE_SECTION_HEADER = 0;

    // Attributes
    private Context mContext;
    private BaseAdapter mListAdapter;
    private int mSectionHeaderLayoutId;
    private int mSectionTitleTextViewId;
    private Sectionizer<T> mSectionizer;
    private LinkedHashMap<String, Integer> mSections;
    AlphabetIndexer alphaIndexer;
    private Filter filter;



    /**
     * Constructs a {@linkplain SimpleSectionAdapter}.
     * 
     * @param context The context for this adapter.
     * @param listAdapter A {@link ListAdapter} that has to be sectioned.
     * @param sectionHeaderLayoutId Layout Id of the layout that is to be used for the header. 
     * @param sectionTitleTextViewId Id of a TextView present in the section header layout.
     * @param sectionizer Sectionizer for sectioning the {@link ListView}.
     */
    public SimpleSectionAdapter(Context context, BaseAdapter listAdapter, 
            int sectionHeaderLayoutId, int sectionTitleTextViewId, 
            Sectionizer<T> sectionizer) {

        if(context == null) { 
            throw new IllegalArgumentException("context cannot be null.");
        } else if(listAdapter == null) {
            throw new IllegalArgumentException("listAdapter cannot be null.");
        } else if(sectionizer == null) {
            throw new IllegalArgumentException("sectionizer cannot be null.");
        } else if(!isTextView(context, sectionHeaderLayoutId, sectionTitleTextViewId)) {
            throw new IllegalArgumentException("sectionTitleTextViewId should be a TextView.");
        }

        this.mContext = context;
        this.mListAdapter = listAdapter;
        this.mSectionHeaderLayoutId = sectionHeaderLayoutId;
        this.mSectionTitleTextViewId = sectionTitleTextViewId;
        this.mSectionizer = sectionizer;
        this.mSections = new LinkedHashMap<String, Integer>();

        // Find sections
        findSections();
    }

    private boolean isTextView(Context context, int layoutId, int textViewId) {
        View inflatedView = View.inflate(context, layoutId, null);
        View foundView = inflatedView.findViewById(textViewId);

        return foundView instanceof TextView;
    }

    @Override
    public int getCount() {
        return mListAdapter.getCount() + getSectionCount();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        SectionHolder sectionHolder = null;

        switch (getItemViewType(position)) {
        case VIEW_TYPE_SECTION_HEADER:
            if(view == null) {
                view = View.inflate(mContext, mSectionHeaderLayoutId, null);

                sectionHolder = new SectionHolder();
                sectionHolder.titleTextView = (TextView) view.findViewById(mSectionTitleTextViewId);

                view.setTag(sectionHolder);
            } else {
                sectionHolder = (SectionHolder) view.getTag();
            }
            break;

        default:
            view = mListAdapter.getView(getIndexForPosition(position), 
                    convertView, parent);
            break;
        }

        if(sectionHolder != null) {
            String sectionName = sectionTitleForPosition(position);
            sectionHolder.titleTextView.setText(sectionName);
        }

        return view;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return mListAdapter.areAllItemsEnabled() ? 
                mSections.size() == 0 : false;
    }

    @Override
    public int getItemViewType(int position) {
        int positionInCustomAdapter = getIndexForPosition(position);
        return mSections.values().contains(position) ? 
                VIEW_TYPE_SECTION_HEADER : 
                    mListAdapter.getItemViewType(positionInCustomAdapter) + 1;
    }

    @Override
    public int getViewTypeCount() {
        return mListAdapter.getViewTypeCount() + 1;
    }

    @Override
    public boolean isEnabled(int position) {
        return mSections.values().contains(position) ? 
                false : mListAdapter.isEnabled(getIndexForPosition(position));
    }

    @Override
    public Object getItem(int position) {
        return mListAdapter.getItem(getIndexForPosition(position));
    }

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

    @Override
    public void notifyDataSetChanged() {
        mListAdapter.notifyDataSetChanged();
        findSections();
        super.notifyDataSetChanged();
    }

    /**
     * Returns the actual index of the object in the data source linked to the this list item.
     * 
     * @param position List item position in the {@link ListView}.
     * @return Index of the item in the wrapped list adapter's data source.
     */
     public int getIndexForPosition(int position) {
         int nSections = 0;

         Set<Entry<String, Integer>> entrySet = mSections.entrySet();
         for(Entry<String, Integer> entry : entrySet) {
             if(entry.getValue() < position) {
                 nSections++;
             }
         }

         return position - nSections;
     }

     static class SectionHolder {
         public TextView titleTextView;
     }

     private void findSections() {
         int n = mListAdapter.getCount();
         int nSections = 0;
         mSections.clear();

         for(int i=0; i<n; i++) {
             String sectionName = mSectionizer.getSectionTitleForItem((T) mListAdapter.getItem(i));


             if(!mSections.containsKey(sectionName)) {
                 mSections.put(sectionName, i + nSections);
                 nSections ++;
             }
         }

         if(DEBUG) {
             Log.d(TAG, String.format("Found %d sections.", mSections.size()));
         }
     }

     private int getSectionCount() {
         return mSections.size();
     }

     private String sectionTitleForPosition(int position) {
         String title = null;

         Set<Entry<String, Integer>> entrySet = mSections.entrySet();
         for(Entry<String, Integer> entry : entrySet) {
             if(entry.getValue() == position) {
                 title = entry.getKey();
                 break;
             }
         }

         return title;
     }

您可以使用addTextChangedListeneronTextChanged方法獲取搜索字符串onTextChanged此字符串與listItems匹配並准備新的搜索項目列表。然后重新初始化適配器並將其設置為ListView

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM