Use of Android open-source class library Pinyin4j ---- search contacts

Source: Internet
Author: User
Tags getsimple

Use of Android open-source class library Pinyin4j ---- search contacts

Pinyin4j can only be regarded as a Java open source class library, but it is also frequently used in Android application development. You have used mobile phone address book and address book to search for contacts. You can use pinyin4j. The following example shows how to use pinyin4j to search for contacts.

First, let's give a brief introduction to pinyin4j:

Pinyin4j is a popular Java library that supports conversion between Chinese characters and pinyin. The output format of Pinyin can also be customized. It also supports the multiphoneme of Chinese characters. The official website of pinyin4j is: http://pinyin4j.sourceforge.net/

The user manual of pinyin4j is a document in Baidu Library. To learn more about the use of pinyin4j, click the user manual of pinyin4j.

Before proceeding, let's take a look at this case:




Next let's take a look at the project directory:


In this case, there are two classes in total. pinyymol is a tool class and its code is as follows:

Package com. gc. testcontactpeopledemo; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; import net. sourceforge. pinyin4j. pinyinHelper; import net. sourceforge. pinyin4j. format. hanyuPinyinOutputFormat; import net. sourceforge. pinyin4j. format. hanyuPinyinToneType; import net. sourceforge. pinyin4j. format. hanyuPinyinVCharType; import net. sourceforge. pinyin4j. format. exception. badHanyuPinyinOutputFormatCombination;/***** @ author Android **/public class PinyinTool {public static HanyuPinyinOutputFormat format; public static List
 
  
> List; public static Map
  
   
> Map; public static Map
   
    
PMap; public static List
    
     
StrList;/*** function: Generate pinyin abbreviations and spelling Based on the contact name * @ param strList stores the List of contact names * @ throws BadHanyuPinyinOutputFormatCombination */public static void setData (List
     
      
StrList) throws handler {pinypolicol. strList = strList; format = new inline (); format. setToneType (HanyuPinyinToneType. WITHOUT_TONE); format. setVCharType
      
        >>(); For (int I = 0; I
       
         > (); PMap = new HashMap
        
          (); PMap. put ("simple", getSimple (strList. get (I); pMap. put ("complex", getComplex (strList. get (I); map. put (strList. get (I), pMap); list. add (map) ;}}/*** function: Obtain the pinyin abbreviation * @ param str * @ return */public static String getSimple (String str) of the str String) {StringBuilder sb = new StringBuilder (); String tempSimple = null; for (int I = 0; I
         
           Search (String str) {List
          
            Temp = new ArrayList
           
             (); For (int I = 0; I
            
              The MainActivity code is as follows:
             
Package com. gc. testcontactpeopledemo; import java. util. arrayList; import java. util. list; import net. sourceforge. pinyin4j. pinyinHelper; import net. sourceforge. pinyin4j. format. exception. badHanyuPinyinOutputFormatCombination; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. text. editable; import android. text. textWatcher; import android. view. layoutInflater; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. editText; import android. widget. imageView; import android. widget. listView; import android. widget. textView;/*** function: Search for contacts * @ author Android General **/public class MainActivity extends Activity implements TextWatcher {private EditText mEditText; private ListView mListView; private PinyinHelper mPinyinHelper; private MyAdapter mMyAdapter; private List
              
               
MList; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); setData (); initUI (); setListeners ();}/*** initialize contact data */public void setData () {mList = new ArrayList
               
                
(); MList. add ("Anhui"); mList. add ("Wang xiao'er"); mList. add ("Android General"); mList. add ("Tianjin cannabis flower"); mList. add ("Machang Dao"); mList. add ("Haihe Bay"); mList. add ("5th Avenue"); mList. add ("Suzhou Road"); mList. add (""); mList. add ("");}/*** initialization component */public void initUI () {mEditText = (EditText) findViewById (R. id. search); mListView = (ListView) findViewById (R. id. listview); mMyAdapter = new MyAdapter (); mListView. setAdapter (mMyAdapter); try {pinypolicol. setData (mList);} catch (BadHanyuPinyinOutputFormatCombination e) {// TODO Auto-generated catch blocke. printStackTrace () ;}/ *** set listener */public void setListeners () {mEditText. addTextChangedListener (this);}/*** EditText listener event, provides the search function */@ Overridepublic void beforeTextChanged (CharSequence s, int start, int count, int after) {for (int I = 0; I
                
                 

Because the code is relatively simple and some comments are added, I will not explain it too much here. If you still do not understand it, Just reply to the message.

Reprinted please indicate the source: http://blog.csdn.net/android_jiangjun/article/details/40422251

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.