Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: Custom AutoCompleteTextView shows last item after it has been selected
    text
    copied!<p>I've implemented a custom auto complete text view to allow users to select tags; when an item is chosen, it correctly calls the listener to set the selected text in the view and color that corresponds with it. The problem i'm having is that the selected item pops instantly back up as one of the choices; I want the item to go away (and could probably do it by keeping track of the pressed item, but is there a cleaner way to do this or something that i'm missing? </p> <p>Filter:</p> <pre><code> protected class TagFilter extends Filter{ @Override protected FilterResults performFiltering(CharSequence constraint) { FilterResults results = new FilterResults(); if(resultTags == null){ resultTags = new LinkedList&lt;Tag&gt;(); } else { resultTags.clear(); } if (constraint != null) { String tagString = constraint.toString(); for (Tag tag : globalTags) { if (tag.getText().startsWith(tagString)) { resultTags.add(tag); } } if(resultTags.size() &gt; 1) Collections.sort(resultTags); } synchronized (this) { results.values = resultTags; results.count = resultTags.size(); } return results; } @Override protected void publishResults(CharSequence constraint, FilterResults results) { tagAdapter.clear(); if (results.count &gt; 0) { for(Tag tagResult : (List&lt;Tag&gt;)results.values){ tagAdapter.add(tagResult); } tagAdapter.notifyDataSetChanged(); } else { tagAdapter.notifyDataSetInvalidated(); } } } </code></pre> <p>Tag Adapter Code:</p> <pre><code> protected class TagAdapter extends ArrayAdapter&lt;Tag&gt; implements Filterable { @Override public Filter getFilter() { if(tf == null){ tf = new TagFilter(); } return tf; } public TagAdapter(Context context, int textViewResourceId) { super(context, textViewResourceId); } @Override public View getView(int position, View convertView, ViewGroup parent) { TextView text = new TextView(CreateActivity.this); text.setPadding(20, 20, 20, 20); text.setTextSize(20); text.setBackgroundColor(Color.BLACK); text.setTextColor(Color.WHITE); //TODO holder here Tag tag = getItem(position); text.setText(" " + tag.getText(), TextView.BufferType.SPANNABLE); ((Spannable)text.getText()).setSpan( new BackgroundColorSpan(tag.getColor()), 0, 4, 0); return text; } } </code></pre> <p>AutoCompleteTextView Listener:</p> <pre><code> tagText.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3) { Tag selectedTag = resultTags.get((int) arg3); selectedColor = selectedTag.getColor(); tagText.setText(selectedTag.getText()); tagText.dismissDropDown(); } }); </code></pre> <p>Does anyone have an idea?</p>
 

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