Dynamic autocomplete widget in android

Can you kindly explain to me how to implement a dynamic automatic full widget in Android. My requirement is that when you enter the letter, the stream will work and return an array with a maximum of 5 sentences. I need to display these 5 sentences as an automatic complete list.

Experts will tell you how to implement the same.

Expecting, Regards, Roni

+3
source share
1 answer

Have you viewed AutoCompleteTextView?

It displays what you want. Now you need an adapter that implements Filterable to create a set of five. Filterable says that the object can create a Filter object. Filter objects use a thread pool and send filtering to a separate worker thread and bind to a view in the user interface thread.

So let's say we have

public class TextAdapter extends BaseAdapter implements Filterable { List<String> myList; Filter myFilter; TextAdapter(String[] strings) { myList = Arrays.asList(strings); myFilter = new MyFilter(myList); } ... // implement the BaseAdapter methods as needed to manage the list. // public void setContents(List<String> strs) { myList.clear(); myList.addAll(strs); mFilter = new Filter(myList); } public Filter getFilter() { return myFilter; } private final class MyFilter implements Filter { final List<String> mOriginalList; public MyFilter(List<String> list) { mOriginalList = new ArrayList<String>(list); } public Filter.FilterResults performFiltering(CharSequence constraint) { // Search through your original list Filter.FilterResults results = new Filter.FilterResults(); List<String> strs = new ArrayList<String>(); if (TextUtils.isEmpty(constraint)) { strs.addAll(myOriginalList); } for (String str : myOriginalList) { if (matches(str, constraint)) { strs.add(str); } } if (results.size > 5) { // prune the list to your own tastes } results.count = strs.size(); results.value = strs; } public void publishResults(CharSequence constraint, Filter.FilterResults results) setContents((List<String>)results.value); notifyDatasetChanged(); } public boolean matches(String str, CharSequence value) { /// implement this part } } } 
+5
source

Source: https://habr.com/ru/post/904664/


All Articles