Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would indeed go for a custom adapter, in which you provide your own filter function to match on both the accented and deaccented notations.</p> <p>An example implementation doing just that, can be found <a href="https://stackoverflow.com/questions/5023645/how-do-i-use-autocompletetextview-and-populate-it-with-data-from-a-web-api">here</a>. Basically you will need to implement the actual filtering in <code>performFiltering</code> - I'm assuming you already have a way to deaccent queries, since you're currently populating your <code>String[]</code> with deaccented versions. You will want to compare the query with and without accents against the entries in your array (which you will want to use with and without accents). In the end you should have at least the following four tests:</p> <pre><code>accented(query) -&gt; accented(entry) accented(query) -&gt; deaccented(entry) deaccented(query) -&gt; accented(entry) deaccented(query) -&gt; deaccented(entry) </code></pre> <p>By deaccenting words on-the-fly, you will only have to provide the <code>String[]</code> with accented words, while the filtering logic (in your adapter) will take care of matching against (de)accented words.</p> <p><strong>Edit:</strong> as discussed, below a sample implementation in one of my ongoing projects.</p> <p>Some pointers:</p> <ol> <li><code>CustomArrayAdapter</code> is mostly a wrapper class that simplifies common tasks; e.g. interaction with a row wrapper/view holder. Basically all it needs is a constructor and implementation of <code>updateRow</code> (which obviously will get called from the super class' <code>getView</code> method).</li> <li><code>CustomRowWrapper</code> should be pretty straightforward.</li> <li><code>ArrayUtil</code> and <code>ArrayUtil.FilterFuction</code> take care of the actual filtering. Simply said these act as replacement for a for loop that builds a new list of all items matching some criteria.</li> </ol> <hr> <pre><code>public class CARMedicationSuggestionAdapter extends CustomArrayAdapter&lt;CARMedicationInfo, RowWrapper&gt; { private List&lt;CARMedicationInfo&gt; mMedications; private Filter mFilter; /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * constructor * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ public CARMedicationSuggestionAdapter(Context context, List&lt;CARMedicationInfo&gt; objects) { super(RowWrapper.class, context, R.layout.medication_suggestion_item_layout, objects); // keep copy of all items for lookups mMedications = new ArrayList&lt;CARMedicationInfo&gt;(objects); } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * update row * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ @Override protected void updateRow(RowWrapper wrapper, CARMedicationInfo item) { wrapper.getNameTextView().setText(item.toString()); } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * get filter * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ @Override public Filter getFilter() { // return if already created if (mFilter != null) return mFilter; mFilter = new Filter() { @Override protected void publishResults(CharSequence constraint, FilterResults results) { @SuppressWarnings("unchecked") List&lt;CARMedicationInfo&gt; filtered = (List&lt;CARMedicationInfo&gt;) results.values; if (results == null || results.count == 0) return; // clear out current suggestions and add all new ones clear(); addAll(filtered); } @Override protected FilterResults performFiltering(final CharSequence constraint) { // return empty results for 'null' constraint if (constraint == null) return new FilterResults(); // get all medications that contain the constraint in drug name, trade name or whose string representation start with the constraint List&lt;CARMedicationInfo&gt; suggestions = ArrayUtil.filter(mMedications, new ArrayUtil.FilterFunction&lt;CARMedicationInfo&gt;() { @Override public boolean filter(CARMedicationInfo item) { String query = constraint.toString().toLowerCase().trim(); return item.mMedicationDrugName.toLowerCase().contains(query) || item.mMedicationTradeName.toLowerCase().contains(query) || item.toString().toLowerCase().startsWith(query); } }); // set results and size FilterResults filterResults = new FilterResults(); filterResults.values = suggestions; filterResults.count = suggestions.size(); return filterResults; } }; return mFilter; } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * row wrapper * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ static class RowWrapper extends CustomRowWrapper { private ImageView mIconImageView; private TextView mNameTextView; public RowWrapper(View row) { super(row); } public ImageView getIconImageView() { if (mIconImageView == null) mIconImageView = (ImageView) mRow.findViewById(R.id.icon_imageview); return mIconImageView; } public TextView getNameTextView() { if (mNameTextView == null) mNameTextView = (TextView) mRow.findViewById(R.id.name_textview); return mNameTextView; } } } </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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