Skip to content Skip to sidebar Skip to footer

How Do I Use Publishresults() Method When Extending Filters In Android?

I'm working on an autocompletetextview that will work off of a key value system, and am trying to find out what I need to do to make publishResults work, as the results param being

Solution 1:

First of all don't use String array.

to work for key value pair you can adjust your If statement.. try this in your onCreate

AutoCompleteTextView mAutoCompleteTextView;
ArrayList<String> lWds = newArrayList<String>();
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mAutoCompleteTextView=(AutoCompleteTextView)findViewById(R.id.testAutoComplete);


    final AutoCmpAdapter adapter= newAutoCmpAdapter(this, android.R.layout.simple_dropdown_item_1line,lWds);
    mAutoCompleteTextView.setAdapter(adapter);
    mAutoCompleteTextView.addTextChangedListener(newTextWatcher() {

        @OverridepublicvoidonTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @OverridepublicvoidbeforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @OverridepublicvoidafterTextChanged(Editable s) {
            adapter.getFilter().filter(s);

        }
    });
}

and adapter class like

publicclassAutoCmpAdapterextendsArrayAdapter<String> implementsFilterable {

    protectedFilter filter;
    protectedArrayList<String> items;
    protectedArrayList<String> res;

    String sWds[] = { "SIMPSON", "JONES" };

    publicAutoCmpAdapter(Context context, int textViewResourceId,ArrayList<String> listData) {
        super(context, textViewResourceId,0,listData);

        filter = newPhysFilter();
        res = newArrayList<String>();
    }

    publicFiltergetFilter() {
        return filter;
    }

    privateclassPhysFilterextendsFilter {

        @OverrideprotectedFilterResultsperformFiltering(CharSequence constraint) {
            FilterResults f = newFilterResults();
            res.clear();
            if (constraint != null) {
                ArrayList<String> res = newArrayList<String>();
                for (int x = 0; x < sWds.length; x++) {
                    if (sWds[x].toUpperCase().contains(constraint.toString().toUpperCase())) {
                        res.add(sWds[x]);
                    }
                }
                f.values = res;//.toArray();
                f.count = res.size();
            }
            return f;
        }

        @SuppressWarnings("unchecked")
        @OverrideprotectedvoidpublishResults(CharSequence constraint, FilterResults results) {
            if (results.count > 0) {
                Log.println(Log.INFO, "Results", "FOUND");
                lWds.clear();
                lWds.addAll((ArrayList<String>) results.values);
                notifyDataSetChanged();
            } else {
                Log.println(Log.INFO, "Results", "-");
                notifyDataSetInvalidated();
            }
        }
    }
}

Post a Comment for "How Do I Use Publishresults() Method When Extending Filters In Android?"