Android EditText + ListView Implementation Search for content in ListView

Source: Internet
Author: User

I used to think that something was in the edittext. You can then detect the contents of the ListView very tall. I've never tried it. Now I've met you in the project. The results found quite simple, the effect is good, mainly to use the EditText TextChange monitoring and filtering of the ListView. Directly below:





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>

Activity

Package Com.example.testedittextlistview;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 ;p rivate ImageButton search_clear;private ListView lv_main;//control input box Show 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); final string[] myString = new string[] {" AAAA "," bbbb "," CCCC "," dddd "," I Love You "};final arrayadapter<string> adapter = new Arrayadapter<string> (ge Tapplicationcontext (), Android. r.layout.simple_list_item_1,mystring); Lv_main.setadapter (adapter);//Add a click event for the ListView lv_ Main.setonitemclicklistener (New Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<?> parent , View view,int position, long ID) {//This note uses Adapter.getitem (position)//rather than mystring[position], if you use the second type, The effect is very pit yo Toast.maketext (Getapplicationcontext (), "clicked" + adapter.getitem (position), 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 Implementation Search for content in ListView

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.