According to the EditText search box ListView Dynamic Display data is based on demand, think that the things involved may be more interesting, so write a write. Hope to be a little help to everyone.
First of all. Let's analyze the whole process:
1. Create a layout that includes a edittext search box and a ListView
2. Create a DataSet Mdata, for adapter creation of the ListView
3. Added edittext text change listener
4. Dynamically update the ListView with Notifydatasetchanged ()
First step: Create a search box
This is still easier. Here I am using a text box that http://blog.csdn.net/walker02/article/details/7917392 the article. With the Point fork full delete function, just, delete the search button, because we dynamic search, with no button.
Add a relativelayout layout and add two controls (in detail, 3).
The effect is as above, XML code such as the following:
<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.ricawinter.dynamicsearch.mainactivity$placeholderfragment" > <relativelayout android:id= "@+id/top" and Roid:layout_width= "Fill_parent" android:layout_alignparenttop= "true" android:paddingleft= "10DP" android:paddingRi ght= "10DP" android:background= "@drawable/top_background" android:layout_height= "wrap_content" > < ; Relativelayout android:id= "@+id/rlsearchframedelete" android:layout_width= "Fill_parent" a ndroid:layout_height= "Wrap_content" android:layout_centervertical= "true" android:gravity= "Center_ver Tical "> <edittext android:id=" @+id/etsearch "android:layout_width=" Fill_parent "Androi d:layout_height= "Wrap_content" Android:singlelIne= "true" android:background= "@drawable/search_frame" android:layout_marginright= "10DP" Android:padd ingleft= "32DP" android:textsize= "12SP" android:hint= "Searching ..."/> <imageview android:id= "@+i D/ivdeletetext "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:layo Ut_alignparentright= "true" android:src= "@drawable/delete" android:layout_centerinparent= "true" Androi d:paddingright= "20DP" android:visibility= "Gone"/> </RelativeLayout> < /relativelayout> <listview android:id= "@+id/mlistview" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:layout_alignparentleft= "true" android:layout_below= "@+id/to P "> </ListView></RelativeLayout>
Step Two: Create a DataSet Mdata
The Simpleadapter is used here. So the data set created by the format of my is this, oneself according to their own adapter to build metadata, stored in Mlisttitle and mlisttext data is not to change, and Mdata will be in the text box changes, mdata data will do the corresponding changes, This is what the update operation needs to do. Here is the creation of the metadata set.
The code is as follows:
ListView Mlistview; arraylist<map<string, object>> mdata = new arraylist<map<string, object>> (); arraylist<string> mlisttitle = new arraylist<string> (); arraylist<string> mlisttext = new arraylist<string> (); private void Getmdata (arraylist<map<string, object>> mdatas) { Map<s Tring, object> item = new hashmap<string, object> (); Mlisttitle.add ("This is a title!"); Mlisttext.add ("This is a text.\n2014.09.18.16.33"); item.put ("Ti Tle ", Mlisttitle.get (0)); Item.put (" text ", Mlisttext.get (0)); Mdatas.add (item) ; Mlisttitle.add ("This was an another title!"); Mlisttext.add ("This was an another text.\n2014.09.18.16.33"); IT EM = new Hashmap<strinG, object> (); Item.put ("title", Mlisttitle.get (1)); Item.put ("text", MLISTT Ext.get (1)); Mdatas.add (item); }
And then use Mdata to create adapter
private void Set_mlistview_adapter () { Mlistview = (ListView) Findviewbyid (R.id.mlistview); Getmdata (mdata); adapter = new Simpleadapter (this,mdata,android. R.layout.simple_list_item_2, new string[]{"title", "Text"},new int[]{android. R.id.text1,android. R.ID.TEXT2}); Mlistview.setadapter (adapter); }
To this. The state of the program starts is displayed.
Let's say there's no search box, and that's it.
Step three: Join EditText's text-changing listener
Because we want to dynamically change the display of the ListView. So we have to listen and do the corresponding action.
When the supervisor hears the text change, it uses handler post a runnable to make the corresponding change.
private void Set_esearch_textchanged () {esearch = (EditText) Findviewbyid (R.id.etsearch); Esearch.addtextchangedlistener (New Textwatcher () {@Override public void ontextchanged (Charsequence arg0, int arg1, int arg2, int arg3) {//TODO auto-generated Method Stub//This should be the action to be done at the time of the change, the details are useless. 。 } @Override public void beforetextchanged (Charsequence arg0, int. arg1, int arg2, in T arg3) {//TODO auto-generated Method Stub//This is the action that will run before the text box changes} @Override public void aftertextchanged (Editable s) {//TODO auto-generated method stub/** This is the text box change Will run after the action * because we have to do is, in the text box changes the same time. The data of our ListView is also changed correspondingly. And as shown on the interface.* So here we need to add the data change action.
*/if (s.length () = = 0) {ivdeletetext.setvisibility (View.gone);//When the text box is empty. The fork disappears} else {ivdeletetext.setvisibility (view.visible);//fork is present when the text box is not empty Myhandler.post (echanged); } } }); }
Handler is a great use here, and we are able to base this post function on handler. Can be the interface God horse to do their own desired change, can not only change the ListView. Like every word you enter, the font changes to a color or something.
Fourth step: Dynamically update the ListView with Notifydatasetchanged ()
Back to the point, here is the most important step, we post out, then we will be based on the search text box text and then filter the metadata, and then let the matching data display on the ListView.
(/* ya, top S4 in writing blog, is not a sin ... * *)
Adapter has a method of notifydatasetchanged (). This method is used to update the bound ListView when the data is updated, such as the following:
Enter a 5 or there are two item. My choice is that only the title and text include the characters of the textbox. That is the target item
When you enter 50 o'clock, the text of the search box is included in the text of only one item in the title or TextBox. So just show just the one that's included
Plus a 0. Since the text without item includes 500. So the ListView has no item.
Smooth finish effect, awesome, LZ level too dish. Some problems have been encountered.
Here are some questions you might encounter:
1, notifydatasetchanged (), this update, the Mdata data set has indeed changed, but the ListView is not updated. I was like this before, and later found that the reference to the Mdata data set changed, so adapter again notify useless. Since adapter is bound to a reference to Mdata, the data is not updated to the ListView if the reference is changed. That's why I'm using the Get function to pass a reference in. Assume that you return a reference directly back. Then this problem will arise, so this point needs to be noted.
Be able to participate in http://www.2cto.com/kf/201401/273017.html
2, on the interface UI update, can use handler. By post a runnable to update, runnable will go according to the text of the search box to update the data in the Mdata.
The code is as follows:
Runnable echanged = new Runnable () { @Override public void Run () { //TODO auto-generated method stub Stri ng data = Esearch.gettext (). toString (); Mdata.clear (); Getmdatasub (mdata, data); Adapter.notifydatasetchanged (); }};
3, may have the understanding of mdata a bit of a problem, because after the data update, each time the screening data is placed in the mdata. So the original data, of course, is in Mlisttitle and Mlisttext.
According to the Getmdatasub code that obtains the data, it is known.
private void Getmdatasub (arraylist<map<string, object>> mdatasubs, String data) { int length = Mlisttitle.size (); for (int i = 0; i < length; ++i) { if (mlisttitle.get (i). Contains (data) | | mlisttext.get (i). Contains (data) { map<string,object> item = new hashmap<string,object> (); Item.put ("title", Mlisttitle.get (i)); Item.put ("text", mlisttext.get (i)); Mdatasubs.add (item);}}}
4, because the text box in the first, so the program one executes. The text box gets the focus and then the input method, which uses a linearlayout with a width of 0 in the XML file to get the focus, such as the following code:
<!--to acquire Focus--><linearlayout android:focusable= "true" Android:focusableintouchmode = "true" android:layout_width= "0px" android:layout_height= "0px"/>
Comprehensive. This dynamic approach. It may also need to be improved, such as a filtered method, and the control is just a ListView for sampleadapter, assuming it is a listview of its own definition. It should be able to be optimized again. There is the height of the ListView setting, set to Wrap_content and fill_parent two methods in fact, the real machine is able to experience two kinds of gap. It's more obvious. Is the process of downward stroke.
It's easier to write than to be rude, so don't take offense. Attach a copy of the code
Always like to share, so download for free.
Poke a punch
Android dynamically display data based on EditText search box listview