最近项目中需要用到自动检索内容的需求,即通过改变检索条件来实时更新检索内容。
package com.lolaage.activity.im; import java.util.ArrayList; import java.util.List; import android.content.Context; 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; import com.lolaage.R; import com.lolaage.deamon.Memory; import com.lolaage.entity.Department; import com.lolaage.entity.Team; import com.lolaage.entity.User; public class SearchAdapter extends BaseAdapter implements Filterable{ private LayoutInflater inflater; private Context context; private List<User> list;//保存当前搜索出来的数据 public SearchAdapter(Context ctx,List<User> data){ this.context = ctx; this.list = data; inflater = LayoutInflater.from(context); } @Override public Filter getFilter() {//给listview添加过滤方法,根据名字检索联系人 Filter filter = new Filter() { @Override protected void publishResults(CharSequence constraint, FilterResults results) { list = (List<User>) results.values; if(results.count > 0){ notifyDataSetChanged(); }else { notifyDataSetInvalidated(); } } /** * 返回检索的结果 */ @Override protected FilterResults performFiltering(CharSequence constraint) { FilterResults results = new FilterResults(); List<User> result = new ArrayList<User>(); if(!"".equals(constraint)){ synchronized (Memory.getFriendData()) { for(Team team : Memory.getFriendData()){ List<User> users = team.getFriendList(); for(int i=0;i<users.size();i++){ User user = users.get(i); if(user.getNickName().toString().toLowerCase().contains(constraint)){ result.add(user); } } } } } results.values = result; results.count = result.size(); return results; } }; return filter; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { HoldView holdView = null; if(convertView == null){ holdView = new HoldView(); convertView = inflater.inflate(R.layout.contact_search_item, null); holdView.name = (TextView) convertView.findViewById(R.id.tv_name); holdView.department = (TextView) convertView.findViewById(R.id.tv_department); holdView.phone = (TextView) convertView.findViewById(R.id.tv_phone); convertView.setTag(holdView); }else { holdView = (HoldView) convertView.getTag(); } if(list != null && list.size() > 0){ User user = list.get(position); holdView.name.setText(user.getNickName()); holdView.phone.setText(user.getPhoneNum()); for(int j =0;j<Memory.departments.size();j++){ Department department = Memory.departments.get(j); if(user.getDepartment_id() == Integer.parseInt(department.id)){ holdView.department.setText(department.name); break; } } } return convertView; } class HoldView{ TextView name; TextView department; TextView phone; } }
activity中的处理:
searchAdapter = new SearchAdapter(this, new ArrayList<User>());
etSearch.addTextChangedListener(textWatcher);
etSearch.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// Log.v("LoginActivity", event.getAction()+"");
if (event.getAction() == MotionEvent.ACTION_DOWN) {
elv.setVisibility(View.GONE);
llSearch.setVisibility(View.VISIBLE);//显示检索视图
lvSearch.setAdapter(searchAdapter);
}
return false;
}
});
/**
* 文本输入框输入检索条件的观察者
*/
private TextWatcher textWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// Log.v("LoginActivity", "onTextChanged");
searchAdapter.getFilter().filter(s);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// Log.v("LoginActivity", "beforeTextChanged");
}
@Override
public void afterTextChanged(Editable s) {
// Log.v("LoginActivity", "afterTextChanged");
if ("".equals(etSearch.getText().toString())) {
}
}
};
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="@drawable/group_bg"> <LinearLayout android:id="@+id/rl_contact_search" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_alignParentTop="true"> <EditText android:id="@+id/et_contact_search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:layout_weight="6" android:paddingLeft="45dip" android:background="@drawable/oragi_search" android:hint="search..."/> </LinearLayout> <ExpandableListView android:id="@+id/elv_contact" android:layout_width="fill_parent" android:layout_height="wrap_content" android:cacheColorHint="#00000000" android:drawSelectorOnTop="false"> </ExpandableListView> <RelativeLayout android:id="@+id/ll_search" android:layout_width="fill_parent" android:layout_height="fill_parent" android:visibility="gone" android:orientation="vertical" android:layout_below="@+id/rl_org_search"> <ListView android:id="@+id/list_search" android:layout_alignParentTop="true" android:fadingEdgeLength="0.0sp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawSelectorOnTop="false" android:cacheColorHint="#ffffffff" android:divider="@drawable/poi_add02" android:dividerHeight="1.0dip" android:fastScrollEnabled="true" android:scrollbars="none" /> <ImageButton android:id="@+id/ibBack" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="@drawable/back_btn_selector"/> </RelativeLayout> <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="fill_parent" android:visibility="gone" android:text="no data"> </TextView> </LinearLayout>