Note that there are some explanatory texts on larger screens.

plurals
  1. POApplying search filter to customlist
    primarykey
    data
    text
    <p>I have some data coming from database which I am showing in a list view before that I am arranging the items alphabetically using indexer.So my list is showing the items arranged alphabetically with section headers.Now I want to search items from list using the word entered in editText.How to do that I know we can use filter and textwatcher for that buthow to use that because the adapter I am setting to listview is not arrayadapter but is SimpleSectionAdapter.</p> <pre><code>dbHelper.openDataBase(); Cursor cursor = dbHelper.fetchAllRecords(); Log.v("count of records", cursor.getCount() + ""); ArrayList&lt;CategoryPojo&gt; contents = new ArrayList&lt;CategoryPojo&gt;(); cursor.moveToFirst(); CategoryPojo cp; while (cursor.isAfterLast() == false) { cp = new CategoryPojo(); cp.setCategoryName(cursor.getString(cursor .getColumnIndex(DataBaseHelper.KEY_CATEGORIES))); cp.setCategoryImageName(cursor.getString(cursor .getColumnIndex(DataBaseHelper.KEY_IMAGENAME))); cursor.moveToNext(); contents.add(cp); } cursor.close(); dbHelper.close(); Collections.sort(contents, new Comparator&lt;CategoryPojo&gt;() { @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&lt;CategoryPojo&gt; alphabetSectionizer = new Sectionizer&lt;CategoryPojo&gt;() { @Override public String getSectionTitleForItem(CategoryPojo instance) { return instance.getCategoryName().substring(0, 1); } }; final SimpleSectionAdapter&lt;CategoryPojo&gt; sectionAdapter = new SimpleSectionAdapter&lt;CategoryPojo&gt;( this, adapter, R.layout.section_header, R.id.title,alphabetSectionizer); listView.setFastScrollEnabled(true); listView.setTextFilterEnabled(true); listView.setAdapter(sectionAdapter); edtSearch.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { } }); And Simplesectionadapter is </code></pre> <p>public class SimpleSectionAdapter extends BaseAdapter implements Filterable { private ArrayList mOriginalValues, mDisplayedValues;</p> <pre><code>static final boolean DEBUG = false; static final String TAG = SimpleSectionAdapter.class.getSimpleName(); // Constants private static final int VIEW_TYPE_SECTION_HEADER = 0; private Context mContext; private BaseAdapter mListAdapter; private int mSectionHeaderLayoutId; private int mSectionTitleTextViewId; private Sectionizer&lt;T&gt; mSectionizer; private LinkedHashMap&lt;String, Integer&gt; mSections; AlphabetIndexer alphaIndexer; public SimpleSectionAdapter(Context context, ArrayList&lt;CategoryPojo&gt; contents, BaseAdapter listAdapter, int sectionHeaderLayoutId, int sectionTitleTextViewId, Sectionizer&lt;T&gt; 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.mOriginalValues = contents; this.mDisplayedValues = contents; this.mContext = context; this.mListAdapter = listAdapter; this.mSectionHeaderLayoutId = sectionHeaderLayoutId; this.mSectionTitleTextViewId = sectionTitleTextViewId; this.mSectionizer = sectionizer; this.mSections = new LinkedHashMap&lt;String, Integer&gt;(); // 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 mDisplayedValues.size(); //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&lt;Entry&lt;String, Integer&gt;&gt; entrySet = mSections.entrySet(); for (Entry&lt;String, Integer&gt; entry : entrySet) { if (entry.getValue() &lt; 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 &lt; n; i++) { @SuppressWarnings("unchecked") 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&lt;Entry&lt;String, Integer&gt;&gt; entrySet = mSections.entrySet(); for (Entry&lt;String, Integer&gt; entry : entrySet) { if (entry.getValue() == position) { title = entry.getKey(); break; } } return title; } @Override public Filter getFilter() { return new android.widget.Filter() { @SuppressWarnings("unchecked") @Override protected void publishResults(CharSequence constraint, FilterResults results) { mDisplayedValues = (ArrayList&lt;CategoryPojo&gt;) results.values; // has // the // filtered // values notifyDataSetChanged(); // notifies the data with new filtered // values } @Override protected FilterResults performFiltering(CharSequence constraint) { FilterResults results = new FilterResults(); // Holds the // results of a // filtering // operation in // values ArrayList&lt;CategoryPojo&gt; FilteredArrList = new ArrayList&lt;CategoryPojo&gt;(); if (mOriginalValues == null) { mOriginalValues = new ArrayList&lt;CategoryPojo&gt;(mDisplayedValues); } if (constraint == null || constraint.length() == 0) { // set the Original result to return results.count = mOriginalValues.size(); results.values = mOriginalValues; } else { constraint = constraint.toString().toLowerCase(); for (int i = 0; i &lt; mOriginalValues.size(); i++) { String data = String.valueOf(mOriginalValues.get(i) .getCategoryName()); if (data.toLowerCase() .startsWith(constraint.toString())) { FilteredArrList.add(new CategoryPojo( mOriginalValues.get(i).getCategoryName(), mOriginalValues.get(i) .getCategoryImageName())); } } // set the Filtered result to return results.count = FilteredArrList.size(); results.values = FilteredArrList; } return results; } }; } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload