ユーザーがタグを選択できるように、カスタムのオートコンプリート テキスト ビューを実装しました。アイテムが選択されると、リスナーが正しく呼び出され、選択されたテキストがビューに設定され、それに対応する色が設定されます。私が抱えている問題は、選択した項目が選択肢の 1 つとして即座に表示されることです。アイテムを消したいです(おそらく、押されたアイテムを追跡することでそれを行うことができますが、これを行うためのよりクリーンな方法または私が見逃しているものはありますか?
フィルター:
protected class TagFilter extends Filter{
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if(resultTags == null){
resultTags = new LinkedList<Tag>();
}
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() > 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 > 0) {
for(Tag tagResult : (List<Tag>)results.values){
tagAdapter.add(tagResult);
}
tagAdapter.notifyDataSetChanged();
}
else {
tagAdapter.notifyDataSetInvalidated();
}
}
}
タグ アダプター コード:
protected class TagAdapter extends ArrayAdapter<Tag> 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;
}
}
AutoCompleteTextView リスナー:
tagText.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Tag selectedTag = resultTags.get((int) arg3);
selectedColor = selectedTag.getColor();
tagText.setText(selectedTag.getText());
tagText.dismissDropDown();
}
});
誰にもアイデアはありますか?