The last time you were directly using the Arrayadapter and the Simple_list1 layout file that came with the system. But in the development, we basically do not use the simple cell, the following is how to customize their own filters, so that he would like to search what content to search for what content
:
Listview_cell layout:
<?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:orientation=" Horizontal "> <imageview android:id=" @+id/iv_logo "android:layout_width=" Wrap_content "Android:layout_ height= "50DP" android:layout_alignparentleft= "true" android:layout_alignparenttop= "true" android:src= "@drawable/ic_launcher"/> <linearlayout android:layout_width= "Wrap_content" android:layout_height= "50DP" android:orientation= "vertical" > <textview android:id= "@+id/tv_name" Android Oid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "name" android:layout_weight= "1"/> <textview android:id= "@+id/tv_show" android:layout_width= " Wrap_content "Android:layout_height= "Wrap_content" android:text= "I am the fireworks that I am not the same" android:layout_weight= "1"/> </linearl Ayout></linearlayout>
Activity_main layout:
Xml:
<linearlayout 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:orientation= "vertical" tools:context= "com.example.testedittextlistview.MainActivity" > <include layout= "@layout/myedittext"/> <listview android:id= "@+id/lv_main" android:layout_ Width= "Match_parent" android:layout_height= "match_parent" android:background= "#000" > </ Listview></linearlayout>
Listview_adapter:
Package Com.example.testedittextlistview;import Java.util.arraylist;import Java.util.iterator;import Java.util.list;import Android.content.context;import Android.text.textutils;import Android.view.View;import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.filter;import Android.widget.imageview;import Android.widget.textview;public class Mylistviewadapter extends BaseAdapter {// This is the aggregate of our data, in the filter, this should be the same as the private list<people> myforeverlist;//context Menu Private Context context;//This is, The collection of those data that satisfies the requirements when filtering, private list<people> mfilteredarraylist;//This is the private list<people> that needs to be set to adapter Mypeoplelist;public Mylistviewadapter (Context context, list<people> List) {this.context = context; This.myforeverlist = list;//This time to initialize some of our intermediate variables mfilteredarraylist = new arraylist<people> (); mypeoplelist = new Arraylist<people> ();//To get the data we passed over to the collection. Save the integrity of the original collection Mypeoplelist.addall (myforeverlist); @Overridepublic int GetCount () {return mypeoplelist.size();} @Overridepublic people getItem (int position) {return mypeoplelist.get (position);} @Overridepublic long Getitemid (int position) {//TODO auto-generated method Stubreturn position;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {//TODO auto-generated method Stubif (Conve Rtview==null) {Vholder = new Viewholder (); Convertview = view.inflate (context, R.layout.listview_cell, null); VHOLDER.IV _logo = (ImageView) Convertview.findviewbyid (R.id.iv_logo); vholder.tv_name = (TextView) Convertview.findviewbyid ( R.id.tv_name); vholder.tv_show = (TextView) Convertview.findviewbyid (r.id.tv_show); Convertview.settag (VHolder);} else {Vholder = (Viewholder) Convertview.gettag ();} VHolder.iv_logo.setImageResource (resId); VHolder.tv_name.setText (GetItem (position). GetName ()); vholder.tv_ Show.settext (GetItem (position). Getshow ()); return Convertview;} Private Viewholder vholder;class viewholder{public ImageView iv_logo;public TextView tv_name;public TextView tv_show;} Private NamefiltEr namefilter;public namefilter getfilter () {if (namefilter==null) {namefilter = new Namefilter ();} return namefilter;} Filter data Class Namefilter extends filter {//Perform filter @overrideprotected filterresults performfiltering (charsequence charsequence) {Filterresults filterresults = new Filterresults (); if (Textutils.isempty (charsequence)) { Mfilteredarraylist.clear ();//Clear filterresults.values = Myforeverlist;return filterresults;} else {mfilteredarraylist.clear ();//clear for (iterator<people> Iterator = Myforeverlist.iterator (); Iterator.hasnext ();) {People name = Iterator.next (); if (Name.getname (). Contains (charsequence)) {Mfilteredarraylist.add (name);}} Filterresults.values = Mfilteredarraylist;return filterresults;}} Filter results @overrideprotected void Publishresults (charsequence arg0, filterresults results) {mypeoplelist = (arraylist< people>) results.values;if (Results.count > 0) {notifydatasetchanged ();} else {notifydatasetinvalidated ();}}}}
Activity_main.java:
Package Com.example.testedittextlistview;import Java.util.arraylist;import Java.util.list;import Android.app.activity;import Android.content.context;import Android.os.bundle;import Android.text.Editable;import Android.text.textwatcher;import Android.view.menu;import Android.view.menuitem;import Android.view.View;import Android.view.windowmanager;import Android.view.view.onclicklistener;import Android.view.inputmethod.inputmethodmanager;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.arrayadapter;import Android.widget.EditText ; Import Android.widget.imagebutton;import Android.widget.listview;import Android.widget.toast;public class Mainactivity extends Activity {private EditText query;private ImageButton search_clear;private ListView lv_main;// The control input box shows hidden code private Inputmethodmanager Inputmethodmanager; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Initview ();} private void Initview () {inputmethodmanager= (Inputmethodmanager) Getsystemservice (Context.input_method_service); query = (EditText) Findviewbyid (r.id.query); search_clear = (ImageButton) Findviewbyid (r.id.search_clear); Search_ Clear.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//Click to clear the Contents Query.gettext (). Clear (); Hidesoftkeyboard ();//Hide keyboard});//listview initializes the control and enriches some content Lv_main = (ListView) Findviewbyid (R.id.lv_main); list<people> list = new arraylist<people> (), for (int i=1;i<10;i++) {People tpeople = new people (); if (i< 4) {//make content slightly different dot tpeople.setname ("Test" +i); Tpeople.setshow ("Don't Go Home Tonight" +i);} else {tpeople.setname ("HAHA" +i); Tpeople.setshow ("Love Tonight" +i); List.add (tpeople);} Final Mylistviewadapter adapter = new Mylistviewadapter (Getapplicationcontext (), list); Lv_main.setadapter (adapter); /Add Click event Lv_main.setonitemclicklistener for ListView (New Onitemclicklistener () {@Overridepublic void Onitemclick ( Adapterview<?> Parent, View VIew,int position, long ID) {//This note uses Adapter.getitem (position)//rather than mystring[position], and if you use the second type, The effect is very pit yo Toast.maketext (Getapplicationcontext (), "clicked" + adapter.getitem (position). GetName (), Toast.length_short). Show ();}});/ /For EditText add content to change the listener, ontextchanged inside let adapter, set the filter conditions Query.addtextchangedlistener (new Textwatcher () {@ overridepublic void OnTextChanged (charsequence s, int start, int before,int count) {adapter.getfilter (). filter (s); if ( S.length () >0) {search_clear.setvisibility (view.visible);} else {search_clear.setvisibility (View.GONE);}} The following two methods can be directly ignored @overridepublic void beforetextchanged (charsequence s, int start, int count,int after) {} @Overridepublic V OID aftertextchanged (Editable s) {}});} /** * Hide IME Sample code */void Hidesoftkeyboard () {if (GetWindow (). GetAttributes (). Softinputmode! = WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {if (getcurrentfocus () = null) Inputmethodmanager.hidesoftinputfromwindow (Getcurrentfocus (). Getwindowtoken (), inputmethodmanager.hide_not_ always);}}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Edittext+listview advanced to search for content in the ListView customize your own filters