Android ListView Dynamic display of data according to the EditText search box _android

Source: Internet
Author: User
Tags stub

According to the EditText search box ListView Dynamic display of data is based on demand, feel that the things involved may be more interesting, so start to write a write, I hope to help you a bit.

First, let's analyze the whole process:

1, create a layout, including a edittext search box and a ListView

2, create a dataset mdata, for the creation of ListView adapter

3, add the EditText text change listener

4, the use of notifydatasetchanged () Dynamic Update listview

First step: Create a search box

This is still relatively easy, here I am using the text box of the article, with the http://blog.csdn.net/walker02/article/details/7917392, but delete the search button, because we search dynamically, The button is not available.

Add a relativelayout layout, then add two controls to it (3, in particular),

As an effect, the XML code is as follows:

Step Two: Create a DataSet Mdata

Here is the simpleadapter, so the format of the dataset creation I am like this, I 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, The Mdata data will be changed accordingly, which 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<string, object= "" > item = NE
    W hashmap<string, object= "" > ();
    Mlisttitle.add (This is a title!);
 
    Mlisttext.add (This is a text. 2014.09.18.16.33);
    Item.put (title, Mlisttitle.get (0));
    Item.put (text, mlisttext.get (0));
    Mdatas.add (item);
    Mlisttitle.add (This is a another title!);
  
    Mlisttext.add (This is a another text. 2014.09.18.16.33);
    item = new hashmap<string, object= "" > ();
    Item.put (title, Mlisttitle.get (1));
    Item.put (Text, Mlisttext.get (1));
  Mdatas.add (item); }</string,></string,></string,></map<string,></string></string&Gt;</string></string></map<string,></map<string,>
 

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);
}

By this, the state of the program's beginning is displayed. If there is no search box, this is OK.

Step three: Add the EditText text change listener

Because we want to dynamically modify the display of ListView, so we have to listen, and then do the appropriate action. When the supervisor hears the text change, use handler post a runnable to make the corresponding change.

 private void set_esearch_textchanged () {esearch = (edittext) Findviewbyid (R.id.etsearc
   
 h); Esearch.addtextchangedlistener (New Textwatcher () {@Override public void ontextchanged (charsequence arg0, int a
   RG1, int arg2, int arg3) {//TODO auto-generated Method Stub//This should be a change in the time to do it, the concrete has not been used. @Override public void beforetextchanged (charsequence arg0, int arg1, int arg2, int arg3) {//TODO
    auto-generated method Stub//This is the action that will be performed before the text box changes @Override public void aftertextchanged (Editable s) { 
     TODO auto-generated Method Stub/** This is what happens when the text box changes * Because all we have to do is change the data in our ListView as the text box changes, and as shown in the interface.
     * So here we need to add the data to the modification of the action. */if (s.length () = = 0) {ivdeletetext.setvisibility (View.gone);//When the text box is empty, the fork disappears} else {Ivdeletetex
    T.setvisibility (view.visible);////When the text box is not empty, fork myhandler.post (echanged) appears;
   
}
   }
 }); }

Handler in this embodiment of a huge use, we can according to handler such a post function, can be on the interface God horse to do their own want to change, can be more than just ListView changes, like every word, font changed to another color what, can be.

Fourth step: Dynamically update ListView with Notifydatasetchanged ()

Back to the point, here is the most critical 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 consistent data displayed on the ListView.

(* * ya, S4 in writing blog, is not a sin ... * *)

Adapter has a notifydatasetchanged () method that updates the bound ListView using this method when the data is updated, and the effect is as follows:


Enter a 5 or there are two item, my choice is, as long as the title and text contains text box characters, that is the target item


When you enter 50 o'clock, because only the text of title or text in one item contains the text of the search box, only the one that contains it is displayed


Plus a 0, because the text without the item contains 500, so ListView has no item.

Successful completion of the effect, awesome, LZ level too, encountered a little problem.

Here are some questions you may encounter:

1, notifydatasetchanged (), this update, Mdata dataset also did change, but ListView did not update. 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 Mdata, reference to a change, then the data will not be updated to the ListView. That's why I use the Get function argument to pass the reference in, and if I return directly to a reference, this problem will occur, so this should be noted. can refer to/kf/201401/273017.html

2, on the interface UI updates, you can use handler, through the post of a runnable to update, runnable will go according to the text of the search box to update the Mdata data.

The code is as follows:

Runnable echanged = new Runnable () {
   
 @Override public
 void Run () {
   //TODO auto-generated the method stub
   Stri ng data = Esearch.gettext (). toString ();
    
   Mdata.clear ();
    
   Getmdatasub (mdata, data);
    
   Adapter.notifydatasetchanged ();
    
 }
;

3, may have a mdata understanding of the problem, because the data after the update, each time the screening data are placed in the Mdata, then the original data, of course, is in Mlisttitle and Mlisttext. Based on the Getmdatasub code to get the data.

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);}}
</string,object></string,object></map<string,>

4, because the text box in the first, so the program runs, 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 focus, the code is as follows:

In summary, this dynamic approach may need to be improved, such as filtering methods, and the control here is only for Sampleadapter ListView, if it is a custom ListView, it should be can be optimized. There is the setting of the height of the ListView, set into Wrap_content and fill_parent two methods in fact, the real machine can be experienced two kinds of gap, more obvious it is the process of the downward stroke.

The above is Android according to edittext search box ListView Dynamic Display of data instances, I hope to help friends in need, thank you for your support of this site!

Related Article

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.