Android dynamically displays data based on EditText search box listview

Source: Internet
Author: User
<span id="Label3"></p><p><p>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, I hope that a little help.</p></p><p><p>first, let's analyze the whole process:</p></p><p><p>1. Create a layout that contains a edittext search box and a ListView</p></p><p><p>2. Create a DataSet mdata, for adapter creation of the ListView</p></p><p><p>3. Add edittext text Change listener</p></p><p><p>4. Dynamically update the ListView with Notifydatasetchanged ()</p></p>First step: Create a search box<p><p>Text box, with the Point fork full delete function, however, deleted the search button, because we dynamic search, with no buttons.</p></p><p><p>Add a relativelayout layout, then add two controls to it (specifically 3),</p></p>Step two: Create a DataSet Mdata<p><p>Simpleadapter is used here, so the dataset is created in the format of my own, 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 be changed accordingly, this is the update operation needs to do. Here is the creation of the metadata set,</p></p><p><p>The code is as Follows:</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #000000;"><span style="color: #000000;">ListView mlistview;</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #008080;"><span style="color: #008080;">3</span></span>Arraylist> Mdata =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Arraylist><span style="color: #000000;"><span style="color: #000000;">();</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #008080;"><span style="color: #008080;">5</span></span>ArrayList Mlisttitle =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">ArrayList ();</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span>ArrayList Mlisttext =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">ArrayList ();</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Getmdata (arraylist><span style="color: #000000;"><span style="color: #000000;">Mdatas)</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span>MAP item =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">HashMap ();</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span>Mlisttitle.add (this is a title!<span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span>Mlisttext.add (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;">is <span style="color: #000000;">a Text.</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>2016.01.27.11.20<span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span>Item.put (title, Mlisttitle.get (0<span style="color: #000000;"><span style="color: #000000;">));</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Item.put (text, Mlisttext.get (0<span style="color: #000000;"><span style="color: #000000;">));</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">Mdatas.add (item);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Mlisttitle.add (this was an another title!<span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span>Mlisttext.add (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;">Is an <span style="color: #000000;">another text.</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>016.01.27.11.22<span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #008080;"><span style="color: #008080;"></span> a</span>item =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">HashMap ();</span></span><span style="color: #008080;"><span style="color: #008080;"></span> at</span>Item.put (title, Mlisttitle.get (1<span style="color: #000000;"><span style="color: #000000;">));</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Item.put (text, Mlisttext.get (1<span style="color: #000000;"><span style="color: #000000;">));</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">Mdatas.add (item);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>}</pre><p><p>And then use Mdata to create adapter</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Set_mlistview_adapter ()</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #000000;"><span style="color: #000000;">{</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span>Mlistview =<span style="color: #000000;"><span style="color: #000000;">(ListView) Findviewbyid (r.id.mlistview);</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #000000;"><span style="color: #000000;">Getmdata (mdata);</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #008080;"><span style="color: #008080;">7</span></span>adapter =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Simpleadapter (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">, Mdata,android. r.layout.simple_list_item_2,</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>string[]{title,text},<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">[]{android. R.id.text1,android. r.id.text2});</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #008080;"><span style="color: #008080;">Ten</span></span> <span style="color: #000000;"><span style="color: #000000;">Mlistview.setadapter (adapter);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span>}</pre><p><p>By this, the state of the Program's start is Displayed. If you don't have a search box, you can do it here.</p></p>Step three: add Edittext's text-changing Listener<p><p>Because we want to dynamically modify the display of the listview, we have to listen and then do the corresponding Action. When the supervisor hears the text change, it uses handler post a runnable to make the corresponding change.</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">set_esearch_textchanged ()</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #000000;"><span style="color: #000000;">{</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span>Esearch =<span style="color: #000000;"><span style="color: #000000;">(EditText) Findviewbyid (r.id.etsearch);</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #008080;"><span style="color: #008080;">5</span></span>Esearch.addtextchangedlistener (<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">textwatcher () {</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>OnTextChanged (charsequence arg0,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>arg1,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>arg2,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Arg3) {</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">This should be a change in the time will do the action bar, concrete is not used To. </span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Beforetextchanged (charsequence arg0,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>arg1,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">arg2,</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Arg3) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">This is the action that will be performed before the text box changes</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">aftertextchanged (Editable S) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span><span style="color: #008080;"><span style="color: #008080;"></span> at</span> <span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">This is what happens when the text box Changes.</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">* Because all we have to do is change the text box, and the data of our ListView changes accordingly, and is displayed as if it were on the Interface. </span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">* So here we need to add the data to modify the Action. </span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(s.length () = = 0<span style="color: #000000;"><span style="color: #000000;">){</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Ivdeletetext.setvisibility (view.gone);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">When the <span style="color: #008000;">text box is empty, the fork disappears</span></span><span style="color: #008080;"><span style="color: #008080;"></span> in</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> to</span>Ivdeletetext.setvisibility (view.visible);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">when <span style="color: #008000;">The text box is not empty, a fork appears</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #000000;"><span style="color: #000000;">myhandler.post (echanged);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> *</span> <span style="color: #000000;"><span style="color: #000000;"> });</span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #008080;"><span style="color: #008080;">Panax Notoginseng</span></span>}</pre><p><p>Handler here embodies a huge use, we can according to the handler of such a post function, can be the interface God horse to do their own want to change, can not only be modified by the listview, like every input word, the font changed into another color, can Be.</p></p>Fourth step: dynamically update the ListView with Notifydatasetchanged ()<p><p>Back to the point, here is the most important step, we post out, then we will be based on the text of the search text box and then filter the metadata, and then let the matching data display on the LISTVIEW.</p></p><p><p>(/* ya, top S4 in writing blog, is not a sin ... * *)</p></p><p><p>Adapter has a notifydatasetchanged () method that updates the bound ListView with this method when the data is Updated.</p></p><p><p>Here are some questions you might encounter:</p></p><p><p>1, notifydatasetchanged (), This update, the Mdata data set did change, but the ListView is not Updated. I was like this before, and later found that the reference to the Mdata DataSet changed, so adapter again notify is useless, because adapter is bound with the reference of the mdata, reference a change, then the data will not be updated to the LISTVIEW. That's Why I use the Get function parameter to pass a reference, and if you return a reference directly back, That's the problem, so you need to be Careful. can refer to http://www.2cto.com/kf/201401/273017.html</p></p><p><p>2, on the interface UI update, you can use handler, through post a runnable to update, runnable will go to the text of the search box to update the data in Mdata.</p></p><p><p>The code is as Follows:</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span>Runnable echanged =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Runnable () {</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Run () {</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span>String data =<span style="color: #000000;"><span style="color: #000000;">esearch.gettext (). toString ();</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #000000;"><span style="color: #000000;">mdata.clear ();</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #008080;"><span style="color: #008080;">Ten</span></span> <span style="color: #000000;"><span style="color: #000000;">getmdatasub (mdata, data);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span> <span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #000000;"><span style="color: #000000;">adapter.notifydatasetchanged ();</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>};</pre><p><p>3, may have the understanding of mdata a bit of a problem, because after the data update, each time the screening data are placed in the mdata, then the original data, of course, is in Mlisttitle and Mlisttext. It is known based on the Getmdatasub code that obtains the Data.</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Getmdatasub (arraylist<map<string, object= "" >><span style="color: #000000;"><span style="color: #000000;">mdatasubs, String Data)</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #000000;"><span style="color: #000000;">{</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Length =<span style="color: #000000;"><span style="color: #000000;">mlisttitle.size ();</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>i = 0; I < length; ++<span style="color: #000000;"><span style="color: #000000;">I) {</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(mlisttitle.get (i). contains (data) | |<span style="color: #000000;"><span style="color: #000000;">mlisttext.get (i). contains (data)) {</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span>map<string,object> item =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Hashmap<string,object><span style="color: #000000;"><span style="color: #000000;">();</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #000000;"><span style="color: #000000;">item.put (title, mlisttitle.get (i));</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #000000;"><span style="color: #000000;">item.put (text, mlisttext.get (i));</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #000000;"><span style="color: #000000;">Mdatasubs.add (item);</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span>} </string,object></string,object></map<string,></pre><p><p>4, because the text box in the first, so the program is running, the text box will get the focus, and then pop-up input method, here use in the XML file to add a long width of 0 linearlayout to get the focus, the code is as Follows:</p></p><pre><pre><span style="color: #008080;">1</span> <!--to acquire focus--<span style="color: #008080;">2</span> <<span style="color: #000000;">linearlayout</span><span style="color: #008080;">3</span> android:focusable= "true"<span style="color: #008080;">4</span> android:focusableintouchmode= "true"<span style="color: #008080;">5</span> android:layout_width= "0px"<span style="color: #008080;">6</span> android:layout_height= "0px"/></pre></pre><p><p>In summary, This dynamic approach may also need to be improved, such as the filtering method, and the control is only for Sampleadapter's listview, if it is a custom listview, it should be able to be optimized. There is the setting of the height of the listview, set into Wrap_content and fill_parent two methods actually replaced by real machine can experience two kinds of gap, more obvious bar, is the process of downward stroke.</p></p><p><p></p></p><p><p></p></p><p><p>Android dynamically displays data based on EditText search box listview</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.