Android Autocompletetextview automatic Prompt _android

Source: Internet
Author: User

When we use the control Autocompletetextview automatically prompts, sometimes need to set baseadapter, set Baseadapter, need to implement filterable, manual filtering.

Common Properties

The layout only sets at least one character display prompt

<autocompletetextview
    android:id= "@+id/at"
    android:completionthreshold= "1"
    android:layout_width = "Match_parent"
    android:layout_height= "Wrap_content"/>

 Private list<string> alldata = new arraylist<> ();

  @Override
  protected void onCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (r.layout.activity_main);
    Alldata.add ("AA");
    Alldata.add ("BB");
    Alldata.add ("cc");
    Alldata.add ("DD");
    Alldata.add ("abc");
    Alldata.add ("BCD");
    Autocompletetextview TextView = (autocompletetextview) Findviewbyid (r.id.at);
    Myadapter adapter = new Myadapter ();
    Textview.setadapter (adapter);
  }

Adapter Adapter Code

 Class Myadapter extends Baseadapter implements filterable {Datafilter filter;

    Arraylist<string> data;
      @Override public int GetCount () {if (data!= null && data.size () > 0) {return data.size ();
    return 0;
    @Override public Object getitem (int i) {return null;
    @Override public long Getitemid (int i) {return 0; @Override public View getview (int position, view view, ViewGroup viewgroup) {TextView TextView = new Tex
      Tview (Mainactivity.this);
      Textview.settext (Data.get (position));
    return textView;
      @Override public Filter GetFilter () {if (filter = = NULL) {filter = new datafilter ();
    } return filter; Private class Datafilter extends Filter {@Override protected filterresults performfiltering (charseque NCE prefix) {//Query results saved to Filterresults object filterresults results = new FilterresUlts ();
        list<string> querydata = new arraylist<> (); for (int i = 0; i < alldata.size (); i++) {if (Alldata.get (i). Contains (prefix.tostring ())) {Quer
          Ydata.add (Alldata.get (i));
        } results.values = Querydata;
        Results.count = Querydata.size ();
      return results; /** * Update UI * * * @Override protected void publishresults (charsequence constraint, Filterre
        Sults results) {list<string> Querydata = (list<string>) results.values;
        if (data = = NULL) {data = new arraylist<> ();
        } if (Data.size () > 0) {data.clear ();
            } if (Querydata!= null && querydata.size () > 0) for (String String:querydata) {
            Data.add (string);
          Notifydatasetchanged ();
 }
      }

    }
  }

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.