Android Searchview plus ListView common use

Source: Internet
Author: User

In some apps, it's common to have a edittext below is a ListView interface, and in EditText, you can search for the current listview. Now let's start with Searchview + The ListView way to implement such a function

The main is the use of Searchview onquerytextchange content changes when listening and onquerytextsubmit click on the right of the Searchview to determine the search button monitoring


The main use of the ListView is to allow the ListView to filter lv_main.settextfilterenabled (TRUE);

and add Filter Lv_main.setfiltertext (NewText) to the ListView;//Set filter phrase

Lv_main.cleartextfilter ();//Clear the ListView filter


Self-feeling without EditText + listview in a good way, personally advocating the latter





Xml:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    tools:context= "com.example.testsearchview.MainActivity" >    <searchview        android:id= "@+id/ SearchView1 "        android:layout_width=" match_parent "        android:layout_height=" Wrap_content "        android: Layout_alignparentleft= "true"        android:layout_alignparenttop= "true" >    </SearchView>    < ListView        android:id= "@+id/lv_main"        android:layout_width= "match_parent"        android:layout_height= " Wrap_content "        android:layout_alignleft=" @+id/searchview1 "        android:layout_below=" @+id/searchview1 "        android:background= "#000"        android:layout_margintop= "14DP" >    </listview></ Relativelayout>
Activity

Package Com.example.testsearchview;import Android.app.activity;import Android.os.bundle;import Android.text.textutils;import Android.view.menu;import Android.view.menuitem;import Android.view.View;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.arrayadapter;import Android.widget.listview;import Android.widget.searchview;import Android.widget.toast;import Android.widget.searchview.onquerytextlistener;import Android.widget.SimpleAdapter; public class Mainactivity extends Activity implements Onquerytextlistener {private Searchview searchview1;private ListView lv_main;private final string[] myString = new string[] {"AAAAA", "bbbbb", "CCCCC", "ddddd"}; @Overrideprotected V OID onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main ); Initview ();} private void Initview () {//TODO auto-generated method stubsearchView1 = (Searchview) Findviewbyid (R.ID.SEARCHVIEW1); Searchview1.setquerYhint ("Test Search");//Initialize ListView and enrich data Lv_main = (ListView) Findviewbyid (R.id.lv_main); final arrayadapter<string > adapter = new arrayadapter<> (Getapplicationcontext (), Android. r.layout.simple_list_item_1,mystring); Lv_main.setadapter (adapter); lv_main.settextfilterenabled (true);// Click event Lv_main.setonitemclicklistener of the ListView (New Onitemclicklistener () {@Overridepublic void Onitemclick ( Adapterview<?> parent, View view,int position, long id) {Toast.maketext (Getapplicationcontext (), "clicked" + Adapter.getitem (position), Toast.length_short). Show ();}}); Searchview1.seticonifiedbydefault (false); Searchview1.setonquerytextlistener (this); Searchview1.setsubmitbuttonenabled (True); Searchview1.setqueryhint ("Search");} @Overridepublic boolean onquerytextsubmit (String query) {toast.maketext (Getapplicationcontext (), "you clicked:" + Query, Toast.length_short). Show (); return false;} @Overridepublic boolean onquerytextchange (String newtext) {if (Textutils.isempty (NewText)) {Lv_main.cleartextfilter ( );//Clear LISTVIEW filter} else {lv_main.setfiltertext (newtext);//Set filter phrase}toast.maketext (Getapplicationcontext (), NewText, Toast.length _short). Show (); return true;}}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Searchview plus ListView common use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.