Подтвердить что ты не робот

Изменить фильтр AutoCompleteTextView с "startsWith" на "Содержит"?

Я хочу изменить фильтрацию по умолчанию в AutoCompleteTextView. Фильтрация по умолчанию находит все строки, которые начинаются с данного токена. Мой проект требует, чтобы фильтрация находила все строки, содержащие данный токен.

Возможно ли это?

4b9b3361

Ответ 1

Я нашел решение для этого, благодаря Google и поискать два дня. Как предложил @torque203, я внедрил свой собственный пользовательский адаптер. Сначала определите новый XML файл для пользовательского элемента в адаптере:

autocomplete_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:paddingTop="16dp"
        android:paddingBottom="16dp"
        android:id="@+id/lbl_name" />
</RelativeLayout>

Создайте новый класс для ваших имен:

Имена

public class Names {
    public String name;
}

NamesAdapter

public class NamesAdapter extends ArrayAdapter<Names> {

    Context context;
    int resource, textViewResourceId;
    List<Names> items, tempItems, suggestions;

    public NamesAdapter(Context context, int resource, int textViewResourceId, List<Names> items) {
        super(context, resource, textViewResourceId, items);
        this.context = context;
        this.resource = resource;
        this.textViewResourceId = textViewResourceId;
        this.items = items;
        tempItems = new ArrayList<Names>(items); // this makes the difference.
        suggestions = new ArrayList<Names>();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.autocomplete_item, parent, false);
        }
        Names names = items.get(position);
        if (names != null) {
            TextView lblName = (TextView) view.findViewById(R.id.lbl_name);
            if (lblName != null)
                lblName.setText(names.name);
        }
        return view;
    }

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

    /**
     * Custom Filter implementation for custom suggestions we provide.
     */
    Filter nameFilter = new Filter() {
        @Override
        public CharSequence convertResultToString(Object resultValue) {
            String str = ((Names) resultValue).name;
            return str;
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            if (constraint != null) {
                suggestions.clear();
                for (Names names : tempItems) {
                    if (names.name.toLowerCase().contains(constraint.toString().toLowerCase())) {
                        suggestions.add(names);
                    }
                }
                FilterResults filterResults = new FilterResults();
                filterResults.values = suggestions;
                filterResults.count = suggestions.size();
                return filterResults;
            } else {
                return new FilterResults();
            }
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            List<Names> filterList = (ArrayList<Names>) results.values;
            if (results != null && results.count > 0) {
                clear();
                for (Names names : filterList) {
                    add(names);
                    notifyDataSetChanged();
                }
            }
        }
    };
}

SearchActivity (или ваше основное действие)

....
   List<Names> namesList =  //your names list;
   NamesAdapter namesAdapter = new NamesAdapter(
                    SearchActivity.this,
                    R.layout.activity_search,
                    R.id.lbl_name,
                    namesList
            );
            //set adapter into listStudent
            autoCompleteTextView.setAdapter(namesAdapter);
            autoCompleteTextView.showDropDown();
...

Ответ 2

Да, это возможно.

Первый способ:

Вам следует создать пользовательский адаптер, который реализует ListAdapter и Filterable.

Filter, чем может реализовать логику фильтра "содержит".

И вы установите этот адаптер в качестве адаптера для своего автозаполнения.

Второй способ:

Если вы уже используете ArrayAdapter. Вы можете просто переопределить метод getFilter().

Ответ 3

Просто для того, чтобы расширить приятный ответ Caffe Latte:

1) autoCompleteTextView.showDropDown(); не требуется.

2) Для извлечения входного объекта можно использовать:

//retrieve the input in the autoCompleteTextView
        autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            //parent The AdapterView where the click happened.
            //view The view within the AdapterView that was clicked (this will be a view provided by the adapter)
            //position The position of the view in the adapter
            //id The row id of the item that was clicked.
            public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
                String selection =parent.getItemAtPosition(position).toString();
                Toast.makeText(parent.getContext(),"" + selection,Toast.LENGTH_SHORT).show();
            }
        });

Объект, полученный из родителя, должен иметь метод toString().

Ответ 4

//попробуйте это

autoCompleteText.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View arg1, int position, long arg3) {
        YourCustomModel YourFilterdSelectedModel = (YourCustomModel) adapterView.getItemAtPosition(position);
        autoCompleteText.setText(str);
    }
});

£ Это сработало для меня

Ответ 5

Вот более простая версия Kotlin, которую разместил @Caffe Latte.

Вам не нужен файл пользовательского макета, просто используйте стандартный android.R.layout.simple_list_item_1.

Укажите для этого адаптера любой класс, включая обычные строки. Он просто использует toString() для определения отображаемого текста.

import android.content.Context
import android.widget.ArrayAdapter
import android.widget.Filter
import androidx.annotation.IdRes
import androidx.annotation.LayoutRes
import java.util.*

class AutoCompleteAdapter(
        context: Context,
        @LayoutRes resource: Int,
        @IdRes textViewResourceId: Int = 0,
        internal var items: List<Any> = listOf()
)
    : ArrayAdapter<Any>(context, resource, textViewResourceId, items) {


    internal var tempItems: MutableList<Any> = mutableListOf()
    internal var suggestions: MutableList<Any> = mutableListOf()

    /**
     * Custom Filter implementation for custom suggestions we provide.
     */
    private var filter: Filter = object : Filter() {

        override fun performFiltering(constraint: CharSequence?): FilterResults {
            return if (constraint != null) {
                suggestions.clear()
                tempItems.forEach {
                    if (it.toString().toLowerCase(Locale.getDefault()).contains(constraint.toString().toLowerCase(Locale.getDefault()))) {
                        suggestions.add(it)
                    }
                }

                val filterResults = FilterResults()
                filterResults.values = suggestions
                filterResults.count = suggestions.size
                filterResults
            } else {
                FilterResults()
            }
        }

        override fun publishResults(constraint: CharSequence?, results: FilterResults) {
            val filterList = results.values as? List<Any>
            if (results.count > 0) {
                clear()
                filterList?.forEach {
                    add(it)
                }.also {
                    notifyDataSetChanged()
                }
            }
        }
    }

    init {
        tempItems = items.toMutableList()
        suggestions = ArrayList()
    }

    override fun getFilter(): Filter {
        return filter
    }
}