您好,登錄后才能下訂單哦!
主要原理:是在主界面有兩個空間,一個是EditText,一個是ListView,ListView是放在EditText下面的,然后自定義建立一個adapter適配器,
這個適配器要繼承Filterable這個接口,并實現(xiàn)這個接口的兩個方法,一個是過濾方法,一個是過濾后的方法,一般我們是在過濾方法里面實現(xiàn)過濾,
在過濾后的方法里面調(diào)用notifyDataSetChanged();也就是使適配器更新數(shù)據(jù),這里還有用到一個pinyin4j-2.5.0.jar的架包,這個主要是漢字轉(zhuǎn)拼
音的功能
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("張三" + i); user.setPhonenum("12345" + i); userInfos.add(user); } for (int i = 0; i < 2; i++) { UserInfo user = new UserInfo(); user.setUsername("李四" + i); user.setPhonenum("12345" + i); userInfos.add(user); } lv_list = (ListView) findViewById(R.id.lv_list); adapter = new UserAdapter(MainActivity.this, userInfos); lv_list.setAdapter(adapter); et_filter = (EditText) findViewById(R.id.et_filter); et_filter.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { adapter.getFilter().filter(et_filter.getText().toString()); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { } }); } }
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; /** * 自定義具有過濾功能的adapter類 * */ public class UserAdapter extends BaseAdapter implements Filterable { private MyFilter myFilter; private List<UserInfo> userInfos; private Context context; private ArrayList<UserInfo> mOriginalValues; private final Object mLock = new Object(); public UserAdapter(Context context, List<UserInfo> userInfos) { this.context = context; this.userInfos = userInfos; } @Override public int getCount() { // TODO Auto-generated method stub return userInfos.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return userInfos.get(arg0); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = convertView; ViewHolder holder; if (view == null) { view = LayoutInflater.from(context).inflate(R.layout.list_item, null); holder = new ViewHolder(); holder.tv_nick = (TextView) view.findViewById(R.id.tv_nick); holder.tv_mobile = (TextView) view.findViewById(R.id.tv_mobile); view.setTag(holder); } else { holder = (ViewHolder) view.getTag(); } holder.tv_nick.setText(userInfos.get(position).getUsername()); holder.tv_mobile.setText(userInfos.get(position).getPhonenum()); return view; } static class ViewHolder { TextView tv_nick; TextView tv_mobile; } @Override public Filter getFilter() { if (myFilter == null) { myFilter = new MyFilter(); } return myFilter; } class MyFilter extends Filter { @Override protected FilterResults performFiltering(CharSequence prefix) { // 持有過濾操作完成之后的數(shù)據(jù)。該數(shù)據(jù)包括過濾操作之后的數(shù)據(jù)的值以及數(shù)量。 count:數(shù)量 values包含過濾操作之后的數(shù)據(jù)的值 FilterResults results = new FilterResults(); if (mOriginalValues == null) { synchronized (mLock) { // 將list的用戶 集合轉(zhuǎn)換給這個原始數(shù)據(jù)的ArrayList mOriginalValues = new ArrayList<UserInfo>(userInfos); } } if (prefix == null || prefix.length() == 0) { synchronized (mLock) { ArrayList<UserInfo> list = new ArrayList<UserInfo>( mOriginalValues); results.values = list; results.count = list.size(); } } else { // 做正式的篩選 String prefixString = prefix.toString().toLowerCase(); // 聲明一個臨時的集合對象 將原始數(shù)據(jù)賦給這個臨時變量 final ArrayList<UserInfo> values = mOriginalValues; final int count = values.size(); // 新的集合對象 final ArrayList<UserInfo> newValues = new ArrayList<UserInfo>( count); for (int i = 0; i < count; i++) { // 如果姓名的前綴相符或者電話相符就添加到新的集合 final UserInfo value = (UserInfo) values.get(i); Log.i("coder", "PinyinUtils.getAlpha(value.getUsername())" + PinyinUtils.getFirstSpell(value.getUsername())); if (PinyinUtils.getFirstSpell(value.getUsername()).startsWith( prefixString) || value.getPhonenum().startsWith(prefixString)||value.getUsername().startsWith(prefixString)) { newValues.add(value); } } // 然后將這個新的集合數(shù)據(jù)賦給FilterResults對象 results.values = newValues; results.count = newValues.size(); } return results; } @Override protected void publishResults(CharSequence constraint, FilterResults results) { // 重新將與適配器相關(guān)聯(lián)的List重賦值一下 userInfos = (List<UserInfo>) results.values; if (results.count > 0) { notifyDataSetChanged(); } else { notifyDataSetInvalidated(); } } } }
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>
以上這篇edittext + listview 實現(xiàn)搜索listview中的內(nèi)容方法(推薦)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。