AutoCompleteTextView adapter, a "hidden" adapter?

I have 3 AutoCompleteTextViews, and I would like to register 2 String [] adapters on them. I am currently doing this:

atw_from.setAdapter(new ArrayAdapter(ctx, android.r.layout.simple_dropdown_item_1line, stages_adapter));

Say my user wants to type “Középmező”, he starts typing “Közé” and he will be asked to select Középmező until it is quite simple. But what if the user is too lazy to gain accents (and many of them are lazy), so he will enter only Kozepmezo, then he will not receive any suggestion, since in my String there is no Cosepmezo. What I want, if he types “Kose”, he should be offered Középmez , therefore, even if he does not use accents, he will always be offered the actual word with accents.

I currently have a rather stupid solution, I have String [] with double the size of the original [], the first half contains words with accents, the second contains scattered versions. So now, if he gets Köze, he will be offered Középmező, and if he gets Köze, he will be offered Kozepmezo. It works because the server can handle both versions, but it just looks dumb and I want to solve it.

From what I understand, I have to make a full user adapter. Is this a better approach, or is there any solution included in the SDK? If I have to make a custom adapter, can someone point me in the right direction how to do this? :)

EDIT: added my own answer, should work for everyone, welcomes another answer that directed me in a good direction!

+5
3

Okey, , , . , , .

ctrl + c, ctrl + v BaseAdapter ctrl + c, ctrl + v ArrayAdapter. ArrayFilter, performFiltering. ( !) , , .replace( "x", "y" ) .

, , (, ), , / , . , Google ...

: ctrl + c ctrl + v BaseAdapter, , , , , , .

+1

, , .

, , . performFiltering. , , String[] . ( ). :

accented(query) -> accented(entry)
accented(query) -> deaccented(entry)
deaccented(query) -> accented(entry)
deaccented(query) -> deaccented(entry)

" ", String[] , ( ) (de) .

:, , .

:

  • CustomArrayAdapter - -, ; / . , , updateRow (, , getView).
  • CustomRowWrapper .
  • ArrayUtil ArrayUtil.FilterFuction . , for, , .

public class CARMedicationSuggestionAdapter extends CustomArrayAdapter<CARMedicationInfo, RowWrapper> {

    private List<CARMedicationInfo> mMedications;
    private Filter mFilter;

    /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
     * constructor
     * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

    public CARMedicationSuggestionAdapter(Context context, List<CARMedicationInfo> objects) {
        super(RowWrapper.class, context, R.layout.medication_suggestion_item_layout, objects);
        // keep copy of all items for lookups 
        mMedications = new ArrayList<CARMedicationInfo>(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<CARMedicationInfo> filtered = (List<CARMedicationInfo>) 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<CARMedicationInfo> suggestions = ArrayUtil.filter(mMedications, new ArrayUtil.FilterFunction<CARMedicationInfo>() {
                    @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;
        }

    }

}
+1

All Articles