Android:自定义AutoCompleteTextView显示选择后的最后一项

时间:2011-10-12 03:31:41

标签: android android-layout android-widget custom-controls autocompletetextview

我已经实现了自定义自动完整文字视图,以允许用户选择标签;选择项目时,它会正确调用侦听器以在视图中设置所选文本以及与其对应的颜色。我遇到的问题是所选项目立即弹出作为选择之一备份;我希望物品消失(并且可能通过跟踪按下的物品来做到这一点,但是有更清洁的方法来做这个或我缺少的东西吗?

过滤器:

    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();
        }


    });

有没有人有想法?

0 个答案:

没有答案