Android Search automatically matches keywords and is marked red

Source: Internet
Author: User


This effect is mainly to focus on the performance of search keywords.


1. Single keyword match

If just match the search content can be written simple, the code is as follows:


if (name = null && name.contains (Mkeyword)) {              int index = Name.indexof (Mkeyword);              int len = Mkeyword.length ();              spanned temp = html.fromhtml (name.substring (0, index)                      + "<font color= #FF0000 >"                      + name.substring (index, Index + len) + "</font>"                      + name.substring (index + len, Name.length ()));              Holder.tv_name.setText (temp);          } else {              holder.tv_name.setText (name);          }  


The above name is the one you want to display the entire item content, Mkeyword is the keyword of the search holder.tv_name is the current TextView control



2. Multi-keyword Matching


Sometimes when we do the search is the need to user input keywords in the server to do the split, split into multiple keywords to search. When the server returns data, the keyword is split into multiple. That is, a key word group is returned.


In this case we are going to write an algorithm when the client does a keyword match, to match the multiple keywords in the paragraph and mark them all.


The code is as follows:


Adapter Inside Logic:


/**name   is the item display content  Current item display string content Keylist refers to a list collection that holds multiple keywords */stringbuffer str = new StringBuffer (""); str =  Utils.addchild (name, keylist, str);        Holder.contentTv.setText (html.fromhtml (str.tostring ()));


Matching method:

/** * Multi-keyword query table red, avoid the following keywords become a special HTML language code * @param str Search results * @param inputs keyword Collection * @param resstr table Red results */public static Stringbu Ffer addChild (String str,list<string> inputs,stringbuffer resstr) {int index=str.length ();//used as an identifier, Judge the keyword subscript string next= "";//Save the First keyword found in str for (int i = Inputs.size ()-1; i>= 0;i--) {String thenext=inputs.get (i); int the Index=str.indexof (Thenext); if (theindex==-1) {//filter out invalid keyword inputs.remove (i);} else if (theindex<index) {index=theindex;//replaces subscript next=thenext;}} If the condition is true, there is no keyword in the string that can be replaced, otherwise recursion is handled if (Index==str.length ()) {resstr.append (str);} Else{resstr.append (str.substring (0,index)) resstr.append ("<font color= ' #FF0000 ' >" +str.substring (Index, Index+next.length ()) + "</font>"); String str1=str.substring (Index+next.length (), str.length ()); AddChild (STR1,INPUTS,RESSTR);//The remaining string continues to replace}return Resstr;}



Hope can help everyone. If there is a problem, you can add my QQ group, talk to each other Exchange: Android development experience Exchange Group 454430053


Android Search automatically matches keywords and is marked red

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.