简体   繁体   English

Android AutoCompleteTextView用于提及人

[英]Android AutoCompleteTextView For Mentioning People

currently I am working with android to develop a social media apps. 目前,我正在与android合作开发社交媒体应用。 In this application, I would like to allow the user to mention each other (like in twitter, fb, instagram, etc). 在此应用程序中,我想允许用户互相提及(例如在twitter,fb,instagram等中)。 I have tried to use AutoCompleteTextView. 我试图使用AutoCompleteTextView。 However, it only show suggestion for the first words, not for the next one. 但是,它仅显示建议的第一个单词,而不显示下一个单词。

For example, in current system: If I type: "@ab" for the first word, then it will show the list of suggestions. 例如,在当前系统中:如果我输入:第一个单词“ @ab”,那么它将显示建议列表。 But, when I type: "hi, @ab", then it will not show any suggestions. 但是,当我键入:“ hi,@ab”时,它将不会显示任何建议。

Anyone know how to fix this and provides some example? 任何人都知道如何解决此问题并提供一些示例吗? Here is how I create the AutoCompleteTextVIew in my apps: 这是我在应用程序中创建AutoCompleteTextVIew的方法:

AutoCompleteTextView et_comment = new AutoCompleteTextView(context);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
                    android.R.layout.simple_dropdown_item_1line, user_follow);
et_comment.setAdapter(adapter);

Thanks a lot. 非常感谢。

You need to override getFilter() method. 您需要重写getFilter()方法。

    adapterSearchProducts = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_1, listProductName) {
        @Override
        public Filter getFilter() {
            // TODO Auto-generated method stub
            Log.i(TAG, "getFilter()");
            return myFilter;
        }
    };

Giving you the concept filter class, do some magic with in it with strings. 给您概念过滤器类,并在其中使用字符串做一些魔术。

    /*
     * Custom filter
     */
    Filter myFilter = new Filter() {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            Log.i(TAG, "constraint = " + constraint);

            FilterResults filterResults = new FilterResults();


            /**
            * Do your work here. Play with constraint.
            * Make tmp array list and work with it
            */

            // following two lines is very important
            // as publish result can only take FilterResults objects
            synchronized (this) {
            filterResults.values = "array-list";
            filterResults.count = "array-list-size";
            Log.i(TAG, "filterResults.count ==  " + filterResults.count);
            }

            return filterResults;
        }

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

            if (results != null && results.count > 0) {
                Log.i(TAG, "results.count > 0 :: " + results.count);

                listProductName.clear(); // clear your original array list

                listProductName.addAll((List<String>) results.values);
                // add all tmp result to original result

                adapterSearchProducts.notifyDataSetChanged();
                // notify your data set change
            } else {
                Log.i(TAG, "results.count <= 0  :: " + results.count);
                adapterSearchProducts.notifyDataSetInvalidated();
            }
        }
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM