Custom AutoCompleteTextView for android Development
AutoCompleteTextView is used by many people. In some cases, ArrayAdapter provided by Google can be used as an adapter to meet the requirements. However, in actual development, we often need to develop custom adapters to complete development.
Let's take a look at how to customize such an AutoCompleteTextView today.
Suppose we have a Book class. Book has four attributes: id, name, author, price, and pinyin. I want to match these five attributes in AutoCompleteTextView no matter what characters are entered, if there is a match, the data is displayed. The effects to be achieved are as follows:
The focus of implementing such a function is to override the Adapter. We define an Adapter that inherits from the BaseAdapter and implement the filter Filterable.
Inheriting BaseAdapter is very easy to write. It is often used in ListView. It is the same here. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwPrzMs9BCYXNlQWRhcHRlctb30qrKtc/hybrid + MC0tcTK/b7d1LShozwvcD4NCjxwcmUgY2xhc3M9 "brush: java;"> @Override public int getCount() { return books.size(); } @Override public Object getItem(int position) { return books.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; if (convertView == null) { viewHolder = new ViewHolder();// convertView = LayoutInflater.from(context).inflate(// R.layout.act_item, null); convertView = View.inflate(context, R.layout.act_item, null); viewHolder.id = (TextView) convertView.findViewById(R.id.id_book); viewHolder.name = (TextView) convertView .findViewById(R.id.name_book); viewHolder.author = (TextView) convertView .findViewById(R.id.author_book); viewHolder.price = (TextView) convertView .findViewById(R.id.price_book); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } Book book = books.get(position); viewHolder.id.setText(book.getId() + ); viewHolder.name.setText(book.getName()); viewHolder.author.setText(book.getAuthor()); viewHolder.price.setText(book.getPrice()+); return convertView; } class ViewHolder { TextView id, name, author, price; }
The act_item layout file is as follows:
At the same time, because we have implemented the Filterable interface, we also need to implement a method in this interface:
@Override public Filter getFilter() { if(mArrayFilter==null){ mArrayFilter = new ArrayFilter(); } return mArrayFilter; }
ArrayFilter is a key class for data filtering. This class inherits from the Filter and implements two methods. The first method is the data filtering logic, the second method is to assign the filter result to the data source.
Private class ArrayFilter extends Filter {@ Override protected FilterResults extends mfiltering (CharSequence constraint) {FilterResults results = new FilterResults (); if (mFilterBooks = null) {mFilterBooks = new ArrayList
(Books);} // if no filter condition exists, if (constraint = null | constraint. length () = 0) {results. values = mFilterBooks; results. count = mFilterBooks. size ();} else {List
RetList = new ArrayList
(); // Filter condition String str = constraint. toString (). toLowerCase (); // cyclic variable data source. if an attribute meets the filtering conditions, add it to the result for (Book book: mFilterBooks) {if (book. getAuthor (). contains (str) | book. getName (). contains (str) | (book. getId () + ). contains (str) | (book. getPrice () + ). contains (str) | book. getPinyin (). contains (str) {retList. add (book) ;}} results. values = retList; results. count = retList. size ();} return results;} // The filter result @ Override protected void publishResults (CharSequence constraint, FilterResults results) is returned here {// notifyDataSetInvalidated (), the control will be repainted (restored to the initial state) // notifyDataSetChanged (), repainting the current visible area books = (List
) Results. values; if (results. count> 0) {policydatasetchanged ();} else {policydatasetinvalidated ();}}}
The most important Adapter is ready. Let's see how to call it in MainActivity:
Public class MainActivity extends Activity {private List
Books = new ArrayList
(); Private AutoCompleteTextView act; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); initData (); initView ();} private void initView () {act = (AutoCompleteTextView) this. findViewById (R. id. myact); MyActAdapter adapter = new MyActAdapter (books, this); act. setAdapter (adapter); act. setThreshold (0);} private void initData () {Book b1 = new Book (1, Romance of the Three Kingdoms, Luo Guanzhong, 38, sanguoyanyi); Book b2 = new Book (2, Dream of Red Mansions, cao Xueqin, 25, hongloumeng); Book b3 = new Book (3, travel to the West, Wu chengen, 43, xiyouji); Book b4 = new Book (4, Water Margin, Shi Nai, 72, shuihuzhuan); Book b5 = new Book (5, Suiyuan poetry, Yuan Mei, 32, suiyuanshihua); Book b6 = new Book (6, Jiewen, Xu Shen, 14, shuowenjiezi ); book b7 = new Book (7, wenxindiao long, Liu Jun, 18, wenxindiaolong); books. add (b1); books. add (b2); books. add (b3); books. add (b4); books. add (b5); books. add (b6); books. add (b7 );}}
Layout file:
Public class MainActivity extends Activity {private List
Books = new ArrayList
(); Private AutoCompleteTextView act; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); initData (); initView ();} private void initView () {act = (AutoCompleteTextView) this. findViewById (R. id. myact); MyActAdapter adapter = new MyActAdapter (books, this); act. setAdapter (adapter); act. setThreshold (0);} private void initData () {Book b1 = new Book (1, Romance of the Three Kingdoms, Luo Guanzhong, 38, sanguoyanyi); Book b2 = new Book (2, Dream of Red Mansions, cao Xueqin, 25, hongloumeng); Book b3 = new Book (3, travel to the West, Wu chengen, 43, xiyouji); Book b4 = new Book (4, Water Margin, Shi Nai, 72, shuihuzhuan); Book b5 = new Book (5, Suiyuan poetry, Yuan Mei, 32, suiyuanshihua); Book b6 = new Book (6, Jiewen, Xu Shen, 14, shuowenjiezi ); book b7 = new Book (7, wenxindiao long, Liu Jun, 18, wenxindiaolong); books. add (b1); books. add (b2); books. add (b3); books. add (b4); books. add (b5); books. add (b6); books. add (b7 );}}
Well, it's that simple, but the biggest drawback here is that our data source has a pinyin attribute, which cannot be used in actual development, so how can we search by the first letter of a Chinese character? For example, if you enter sg, the system prompts "Three Kingdoms ". In the next article, I will introduce how to implement such a function through Java classes.