最近项目中需要用到自动检索内容的需求,即通过改变检索条件来实时更新检索内容。
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>
- 大小: 10.9 KB
- 大小: 9.8 KB
分享到:
相关推荐
■Chapter 14: Android Search.......... 491 ■Chapter 15: Exploring Text to Speech and Translate APIs..... 563 ■Chapter 16: Touchscreens ......... 591 ■Chapter 17: Titanium Mobile: A WebKit-Based ...
) or off the Internet, and how to integrate with the built-in on-device search engine. It will help you more quickly climb the Android learning curve, so you can create the “killer app” you’ve ...
■ Chapter 23: Android Search ................................................................................................................... 745 ■ Chapter 24: Exploring Text to Speech .............
主要为大家详细介绍了Android搜索框SearchView的功能和用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
名称:Android SDK Search ---------------------------------------- 版本:0.3.16 作者:http://roman.nurik.net/ 分类:开发者工具 ---------------------------------------- 概述:添加一个'ad'omnibox命令并...
Search Chapter 26. Background Services Chapter 27. Broadcast Intents Chapter 28. Browsing the Web and WebView Chapter 29. Custom Views and Touch Events Chapter 30. Property Animation Chapter 31. ...
Android Oriented Image Visualization Exploratory Search.pdf
安卓Android源码——百度地图搜索Search.zip
安卓Android源码——百度地图 搜索Search.zip
Android应用源码之百度地图 搜索Search.zip
Android应用源码之百度地图 搜索Search.zip项目安卓应用源码下载Android应用源码之百度地图 搜索Search.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术参考
Android-Google-QuickSearchBox 4.x 源码:导入工程后可直接使用
) or off the Internet, and how to integrate with the built-in on-device search engine. It will help you more quickly climb the Android learning curve, so you can create the “killer app” you’ve ...
Android search 从一本英语书上翻译过来的,经过整合的Demo,实现的是Google的搜索功能 进入主界面 按搜索按钮 就可以出现效果
----------------------------------- Android 编程基础 1 封面----------------------------------- Android 编程基础 2 开放手机联盟 --Open --Open --Open --Open Handset Handset Handset Handset Alliance ...
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener; import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption; import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult; import ...
三个android 语音识别例子,开发google语音搜索
Quicklib is a lightweight Android library that provides a bunch of utility classes used to simplify your dev Modules TODO Download Grab via Maven: com.quicklib quicklib-all 1.0.0 or Gradle: ...
Android-QuickSearchBox程序源码.rar