Article From:https://www.cnblogs.com/ganchuanpu/p/6613286.html

The main principle is that there are two spaces in the main interface, one is EditText, the other is ListView, and the ListView is placed under the EditText, and then a custom adapter adapter is created.

The adapter should inherit the Filterable interface and implement the two methods of the interface, one is the filtering method, the other is the filtering method, and we generally implement filtering in the filter method.

NotifyDataSetChanged () is called in the filtered method; that is, to make the adapter update the data, and it is also useful to a pinyin4j-2.5.0.jar package, which is mainly a Chinese character spelling.

The function of sound

MainActivity.Java

public class MainActivity extends Activity {  
    private EditText et_filter;  
    private UserAdapter adapter;  
    private ListView lv_list;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        List<UserInfo> userInfos = new ArrayList<UserInfo>();  
        for (int i = 0; i < 2; i++) {  
            UserInfo user = new UserInfo();  
            user.setUsername("Zhang three "I";User.setPhonenum ("12345" + + I)UserInfos.add (user);""}For (int) I (= 0); I I < (2); i++)UserInfo user new = UserInfo""User.setUsername ("Li four" + I);User.setPhonenum ("12345" + + I)UserInfoS.add (user);""""Lv_list ListView = findViewById (R.id.lv_list);ItAdapter, new, UserAdapter (MainActivity.this, userInfos);Lv_list.setAdapter (adapter);Such""Et_filter EditText = findViewById (R.id.et_filter);Et_filter.addTeXtChangedListener (New TextWatcher)Such@OverridePublic void onTextChaNged (CharSequence, s, int int start, int int before,Int countAdaPter.getFilter ().Filter (et_filter.getText ().ToString ());Such""Such@OverriDePublic void beforeTextChanged (CharSequence s), int int start, int int count,""Int after)As a result""Such@OverridePublIC void afterTextChanged (Editable s)Such""""Such    }  }

UserAdapter.java

package com.example.edittextfilterdemo;  
import java.util.ArrayList;  
import java.util.List;  
import android.content.Context;  
import android.util.Log;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.BaseAdapter;  
import android.widget.Filter;  
import android.widget.Filterable;  
import android.widget.TextView;  
/** 
 * Customizing the adapter class with filtering capabilitiesThatItPublic, class, UserAdapter, extends, BaseAdapter, implements, Filterable, {Private MyFilter myFilter;Private, List< UserInfo> userInfos;Private ContextContextPrivate, ArrayList< UserInfo> mOriginalValues;SuchPrivate final Object MLock = new Object ();SuchPublic UserAdapter (Context context, List<, UserInfo> userInfos)ItThis.context = context;This.userInfos = userInfosSuch""SuchSuch@OverridePublic int getCount ()X / TODO Auto-generated method method stubItReturn userInfos.size ();""Such@OverridePublic Object getItem (int)Arg0)X / TODO Auto-generated method method stubReturn userInfos.get arg0It    }  Such@OverridePublic long getItemId (int position)TODO Auto-Generated method stubReturn position;""Such@OverridePublicView getView (int position, View View convertView, ViewGroup ViewGroup parent)SuchView view = convertVIewSuchViewHolder holder;If (view) (null)View, LayoutINflater.from (context).Inflate (R.layout.list_item,Null.HolderNew ViewHolder ();SuchHolder.tv_nick TextView = view.findViewById (R.id.tv_nick);ItHolder.tv_mobile TextView = view.findViewById (R.id.tv_mobile);View.SetTag (holder);ElseHolder ViewHolder = view.getTag ()"" }  SuchHolder.tv_nick.setText (userInfos.get (position).GetUsername ());Holder.Tv_mobile.setText (userInfos.get (position).GetPhonenum ());SuchReturn view;""ItItStatic class ViewHolderSuchTextView tv_nick;TextView tv_mobile;It""Such@OverridePublic Filter getFilter ()If (myFilter = = = nu)Ll)MyFilter new = MyFilter""Return myFilter;""}SuchClass MyFilter extends extends FilterSuch@OverrideProtected FilterREsults performFiltering CharSequence (prefix)Data from the filtration operation after completion. This data includes the value and quantity of data after filtering operation.The quantity count: values contains the value of data after filtering operation.FilterResults results new = FilterResultsSuch     If (mOriginalValues = = null)Synchronized mLockAs a result/ / convert the list user collection to the ArrayList data of this raw data.MOriginalValues new = ArrayList< UserInf;O> (userInfos);As a result""If prefix (null = prefix.l)Ength () = = = = 0)Synchronized mLockArrayList< UserInfo> list = new ArrayList< UserInfo>MOriginalValues""Results.values and list;Results.count, list.size""   }  ElseMake a formal screening.String prefixString pre =Fix.toString ().ToLowerCase ();SuchThis is a temporary collection of objects, giving the original data to this temporary variable.FiNal; ArrayList< UserInfo> values values = mOriginalValues;SuchFinal int count count = valueS.size ();SuchA new collection of objects.Final ArrayList< UserInfo> newValuesNew ArrayList< UserInfo>CountSuchInt i (I)I < count; i++)If the prefix of the name matches or the telephone matches, add it to the new collection.FinaL, UserInfo, value, UserInfo = values.get (I);Log.i ("coder", "PinyinUtils.getAl")PHA (value.getUsername ())(PinyinUtils.getFirstSpell) (value.getUsername))))As a result, it is very important to do so.If (PinyinUtils.getFirstSpell (value.getUsername)).startsWithPrefixStringValue.getPhoneNum ().StartsWith (prefixString) ||value.getUsername ().StartsWith (prefixString))Such""NewValues.add (value);As a result, it is very important to do so.As a resultThis is what I want to do.The new collection data are assigned to the FilterResults objectResults.values, newValuesResults.counT = newValues.size ();""SuchReturn results;""Such""@OverrideProtected void publishResults (CharSequence constraint)FilTerResults results)List reassign the adapter associated with the adapter.UserInfos List< = UseRInfo>) results.values;SuchIf results.count (> 0)NotifyDatASetChanged ();ElseNotifyDataSetInvalidated ()""}""Such""    }  

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical"  
    tools:context="com.example.edittextfilterdemo.MainActivity" >  
  
    <EditText  
        android:id="@+id/et_filter"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content" />  
  
    <ListView  
        android:id="@+id/lv_list"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content" >  
    </ListView>  
  
</LinearLayout>  

  

  

  

 

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *