Android Custom Filter Search box filterable

Source: Internet
Author: User
<span id="Label3"></p><p><p>Simply put, there is a searchbox on the listview, and then filter the following ListView after entering the contents of the Searchbox.</p></p><p><p>Controls involved: ListView must have, edittext with custom SearchBox</p></p><p><p>That's about It:</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>First look at this has a picture of the EditText, the implementation method has two, one is the relative layout relativelayout + ImageView + EditText.</p></p><p><p>The second is directly with a EditText attribute drawableleft, simple UI This can be achieved</p></p><p><p>So the layout of this activity is very simple and can be implemented with listactivity:</p></p><pre class="brush:java;gutter:true;"><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 "android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: context= ". Mainactivity "> <edittext android:id=" @+id/searchbox "android:layout_width=" wrap_content "an droid:layout_height= "wrap_content" android:layout_alignparenttop= "true" android:drawableleft= "@drawable/sear Chbox "android:hint=" Search "android:drawablepadding=" 5dp "android:singleline=" true "android:e ms= "ten" > <requestfocus/> </EditText> <listview android:id= "@android: id/list" Android:layout_width= "match_parent" android:layout_height= "wrap_content" android:layout_below= "@id/searchbox" > </ListView> </relativelayou T></pre><p><p></p></p><p><p>Again filtering function: This feeling does not want to search, like is simple filter, if involves to go to the database to fetch the data that is searches the</p></p><p><p>Using the filterable interface, the filter class</p></p><p><p>To allow the data to be filtered, we need to implement the GetFilter method of the Filterable interface on the basis of the inherited baseadapter, while writing an internal class that inherits the filter inside adapter to complete the filtering function:</p></p><pre class="brush:java;gutter:true;">Private class ListAdapter extends Baseadapter implements filterable {private list<person> list;private Context cont Ext;private personfilter filter;public listadapter (list<person> list, context Context) {this.list = list; This.context = context;} @Overridepublic int GetCount () {return list.size ();} @Overridepublic Object getItem (int position) {return list.get (position);} @Overridepublic long getitemid (int position) {return position;} @Overridepublic View GetView (int position, view convertview, viewgroup parent) {if (convertview = = Null) {convertview = La Youtinflater.from (context). Inflate (r.layout.list_item, null);} Person p = List.get (position); TextView FirstName = (TextView) Convertview.findviewbyid (r.id.firstname); TextView LastName = (TextView) Convertview.findviewbyid (r.id.lastname); TextView age = (TextView) Convertview.findviewbyid (r.id.age); firstname.settext (p.firstname); lastname.settext ( p.lastname); Age.settext (p.age + ""); return convertview;} @Overridepublic Filter getfilter () {if (filter = = Null) {filter = new Personfilter (list);} Return filter;} Private class Personfilter extends Filter {private list<person> original;public personfilter (list<person> List) {this.original = list;} @Overrideprotected filterresults performfiltering (charsequence constraint) {filterresults results = new Filterresults ( if (constraint = = NULL | | constraint.length () = = 0) {results.values = Original;results.count = original.size ();} Else {L ist<person> mlist = new arraylist<person> (); for (person p:original) {if (p.firstname.touppercase (). StartsWith (constraint.tostring (). touppercase ()) | | P.lastname.touppercase (). startsWith (constraint.tostring (). touppercase ()) | | New String (p.age + ""). touppercase (). startsWith (constraint.tostring (). touppercase ())) {mlist.add (p);}} Results.values = Mlist;results.count = Mlist.size ();} Return results;} @Overrideprotected void Publishresults (charsequence constraint,filterresults results) {list = (list<person>) Results.values;notifydAtasetchanged ();}}} </pre><p><p>Two methods in the filter class the name is to know that one is performing the filtering, and one refreshes the ListView data to show the Result. I used the prefix match, which is to compare the input string with the prefix of the individual attributes of all the person in the LISTVIEW. You can also use more complex matches, such as regular expressions.</p></p><p><p>The key is how the data in the EditText is passed in, write a textwater, and let EditText register the Listener:</p></p><pre class="brush:java;gutter:true;"><pre class="brush:java;gutter:true;"> Private Textwatcher Filtertextwatcher = new Textwatcher () {@Overridepublic void aftertextchanged (Editable S) {}@ overridepublic void beforetextchanged (charsequence s, int start, int count,int after) {} @Overridepublic void Ontextchange d (charsequence s, int start, int before,int count) {listadapter.getfilter (). filter (s);//here the data is available}};</pre></pre><p><p>The key code Above. The non-critical code is the person class and the layout of the List_item:</p></p><pre class="brush:java;gutter:true;"><pre class="brush:java;gutter:true;"> Private class Person {public String firstname;public string lastname;public int age;public person (string firtname, string lastname, int Age) {this.firstname = Firtname;this.lastname = Lastname;this.age = age;}}</pre></pre><p><p>List_item layout:</p></p><pre class="brush:java;gutter:true;"><pre class="brush:java;gutter:true;"><?xml version= "1.0" encoding= "utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "horizontal" > <textview android:id= "@+id/firstname" android:layout_width= "wrap_ Content " android:layout_height=" wrap_content " android:layout_margin=" 10dp " android:text=" Firstname "/> <textview android:id=" @+id/lastname " android:layout_width=" wrap_content " android: layout_height= "wrap_content" android:layout_margin= "10dp" android:text= "Lastname"/> < TextView android:id= "@+id/age" android:layout_width= "wrap_content" android:layout_height= "wrap_ Content " android:layout_margin=" 10dp " android:text=" Age "/></linearlayout></pre></pre><p><p></p></p><p><p>/home/wangjianhua/desktop/1365932013_4825.jpg</p></p><p><p>Android Custom Filter Search box filterable</p></p></span>

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.