Android implements the A-Z alphabet sorting and filtering search functions of listview to convert Chinese characters into pinyin

Going straight to the topic, today we will bring you the A-Z alphabet sorting and filtering search function of listview, and realize the function of converting Chinese characters into pinyin. We know that we generally realize the A-Z sorting of contacts and city lists, because we can directly obtain their Chinese Pinyin from the database. For general data, how do we realize the A-Z sorting, We need to convert Chinese characters into pinyin. Next, we will take you to realize the A-Z sorting function of general data. First, let's take a look at the renderings

Above is an EditText with a delete button. We can automatically filter out what we want by entering in the input box. When there is no data in the input box, it will be automatically replaced with the original data list. Then, the following listview is used to display the data list. On the right is a parent index table. When we click a different letter, the listview will locate the letter, After knowing the layout, let's take a look at the project structure first

I will introduce its functions one by one according to the order of classes in the project

1. Sortmodel is an entity class, in which one is the name of listview and the other is the phonetic initials of the displayed name

2. Sidebar class is the letter index view on the right side of listview. We need to use settextview (textview mtextdialog) to set the textview used to display the currently pressed letter, and setontouchingletterchangedlistener method to set the callback interface to handle different operations in the callback method ontouchingletterchanged (string s)

3. The characterparser class is a class that converts Chinese characters into pinyin. The Pinyin has no tone. This class is a singleton class, in which three methods are defined. In this demo, the getselling (string CHS) method is used to convert phrases into pinyin

4. Clearedittext class is a custom EditText with deleted pictures on the right. Of course, you can also use Android native EditText. I introduced this class before. I won't paste code here. Android input box control with clear function, clearedittext, imitates IOS input box

5. The sortadapter data adapter class needs to implement the sectionindexer interface, which is used to control listview grouping. The interface has three methods: getsectionforposition (int position), getpositionforsection (int section) and getsections(). We only need to implement the first two methods by ourselves

• getsectionforposition (int position) is to obtain the ASCII value of the initial char of the name above the position according to the position of the listview. For example, if the name above the position is a Mei and the initial letter is a, then this method returns the ASCII value of the letter 'a', that is 65, 'B' is 66, and so on • getpositionforsection (int section) It is to obtain the position where the first letter appears in the listview according to the ASCII value of the first letter. For example, from the effect figure 1 above, if the section is 66, that is, the ASCII value of 'B', then the position returned by this method is 2

Then there is the getview () method. First, we call getsectionforposition (int position) according to the position of listview to obtain the ASCII value of the initial letter of name above the position, and then call getpositionforsection (int section) according to the ASCII value to obtain the position where the initial letter appears for the first time, If the position of the listview is equal to calling getpositionforsection (int section) according to the ASCII value to obtain the position where the initial letter appears for the first time, the classification letter is displayed; otherwise, it is hidden

6. The code in mainactivity is relatively simple. We set addtextchangedlistener to listen to clearedittext. When the content of the input box changes, the listview is filtered according to the value inside. If the value inside is empty, the original list is displayed. The pinyincomparator interface is used to sort the list data. This interface is mainly used to compare objects

7. The pinyincomparator interface is used to sort the data in the listview according to A-Z. the first two if judgments are mainly to put the data that does not start with Chinese characters behind

In this way, we will have no limitations when using A-Z sorting in the future. If you want to add this effect at any time, other files such as layouts and pictures will not be posted. If you are interested, download the code yourself. If you have any questions, please leave a message below and I will answer for you!

Project source code, click download

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>