更新MultiAutoCompleteTextView下拉android

时间:2014-09-15 10:12:10

标签: android autocompletetextview android-filter android-filterable multiautocompletetextview

我想在我的Android应用程序中实现用户标记功能。我希望Facebook在评论中使用与用户标签相同的功能。为此我在我的应用程序中实现了自定义MultiAutoCompleteTextView,我还想显示用户图像和用户名。为此,我为MultiAutoCompleteTextView实现了一个带有自定义SimpleAdapter的自定义Filter。我提到this link并实现了这一功能。

我想要实现的更改是,每当我从下拉列表中选择任何项目时,该项目应从下拉列表中删除,并且下拉数据应使用剩余数据进行更新。如果我从AutocompleteText中明确删除了所选项目,则该删除的项目应重新出现在下拉选择框中。我不知道如何实现这一目标。我搜索了很多,但可以得到任何适当的解决方案。请帮我解决这个问题。

谢谢。

活动代码:

    edtCommentTag.setTokenizer(new Tokenizer() {

            @Override
            public CharSequence terminateToken(CharSequence text) {
                int i = text.length();
                while (i > 0 && text.charAt(i - 1) == ' ')
                    i--;
                if (i > 0 && text.charAt(i - 1) == ' ') {
                    return text;
                } else {
                    if (text instanceof Spanned) {
                        SpannableString sp = new SpannableString(text + " ");
                        TextUtils.copySpansFrom((Spanned) text, 0,
                                text.length(), Object.class, sp, 0);
                        return sp;
                    } else {
                        return text + " ";
                    }

                }
            }

            @Override
            public int findTokenStart(CharSequence text, int cursor) {

                int i = cursor;
                while (i > 0 && text.charAt(i - 1) != '@')
                    i--;
                if (i < 1 || text.charAt(i - 1) != '@')
                    return cursor;
                return i;
            }

            @Override
            public int findTokenEnd(CharSequence text, int cursor) {
                int i = cursor;
                int len = text.length();
                while (i < len) {
                    if (text.charAt(i) == ' ')
                        return i;
                    else
                        i++;
                }
                return len;

            }
        });

        adpReply = new CommentAdapter(CommentActivity.this,
                            finalResult, R.layout.liker_comment_row, from, to);

        edtCommentTag.setAdapter(adpReply);

CommentAdapter代码:

public class CommentReplyAdapter extends SimpleAdapter {

    private List<HashMap<String, String>> lstFollowing, suggestions,
            tempLstFollowing;
    RoundedImageView imgUser;
    TextView txtUname;
    private LayoutInflater mInflater;
    Context context;
    private ImageLoader imgLoader;
    private DisplayImageOptions options;
    private Filter nameFilter;

    public CommentReplyAdapter(Context context,
            List<HashMap<String, String>> lstFollowing, int lytId,
            String[] from, int[] to) {

        super(context, lstFollowing, lytId, from, to);

        this.context = context;
        this.lstFollowing = lstFollowing;
        this.tempLstFollowing = new ArrayList<HashMap<String, String>>();
        this.tempLstFollowing.addAll(lstFollowing);
        this.suggestions = new ArrayList<HashMap<String, String>>();

        this.mInflater = LayoutInflater.from(context);
        imgLoader = ImageLoader.getInstance();
        imgLoader.init(ImageLoaderConfiguration.createDefault(this.context));
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.loading_img)
                .showImageForEmptyUri(R.drawable.ic_launcher)
                .showImageOnFail(R.drawable.loading_img).cacheInMemory(true)
                .cacheOnDisc(true).considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .resetViewBeforeLoading(true).build();

    }

    @SuppressWarnings("unchecked")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try {
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.liker_comment_row,
                        null);
            }
            txtUname = (TextView) convertView
                    .findViewById(R.id.txtUserName_likeComment);
            imgUser = (RoundedImageView) convertView
                    .findViewById(R.id.userImage_likeComment);
            HashMap<String, String> data = (HashMap<String, String>) getItem(position);

            txtUname.setText(data.get("uname"));
            imgLoader.displayImage(data.get("avtar"), imgUser, options);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;

    }
@Override
    public Filter getFilter() {
        setUpFilter();
        return nameFilter;

    }

    private void setUpFilter() {

        try {

            nameFilter = new Filter() {

                @SuppressWarnings("unchecked")
                @Override
                public CharSequence convertResultToString(Object resultValue) {

                    String str = ((HashMap<String, String>) resultValue)
                            .get("uname");
                    return str;
                }

                @Override
                protected FilterResults performFiltering(CharSequence constraint) {
                    if (constraint != null) {
                        String newConstraint = constraint.toString();
                        Log.e("newConstraint -- outer", newConstraint);

                        suggestions.clear();
                        for (HashMap<String, String> hm : tempLstFollowing) {
                            String uname = hm.get("uname").toString()
                                    .toLowerCase(Locale.ENGLISH);
                            if (uname.startsWith(newConstraint.toString()
                                    .toLowerCase(Locale.ENGLISH))) {
                                suggestions.add(hm);
                                // hasFound = true;
                            }
                        }

                        FilterResults filterResult = new FilterResults();
                        filterResult.values = suggestions;
                        filterResult.count = suggestions.size();
                        return filterResult;

                    } else {
                        return new FilterResults();
                    }

                }

                @SuppressWarnings("unchecked")
                @Override
                protected void publishResults(CharSequence constraint,
                        FilterResults results) {

                    if (results != null && results.count > 0) {

                        lstFollowing = (List<HashMap<String, String>>) results.values;

                        notifyDataSetChanged();
                    } else {

                        lstFollowing.clear();
                        lstFollowing.addAll(tempLstFollowing);
                    }

                }

            };

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

0 个答案:

没有答案
相关问题