仿SimpleCursorAdapter实现的listview检索,同时支持局部高亮
MainActivity.java
package com.example.autofindfromlist;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.app.Activity;
public class MainActivity extends Activity {
private List<UserInfo> mCacheList = new ArrayList<UserInfo>();
private List<UserInfo> mFindList = new ArrayList<UserInfo>();
private ListView mLv ;
private Button mBt;
private EditText mEt;
private MyAdapter mMyAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initCacheData();
}
private void initView() {
// TODO Auto-generated method stub
mLv = (ListView) findViewById(R.id.listView1);
mEt = (EditText) findViewById(R.id.editText1);
mEt.addTextChangedListener(tw); //自动刷新了
TextView tv;
// tv.addTextChangedListener(watcher);
mBt = (Button)findViewById(R.id.button1);
mMyAdapter = new MyAdapter(getApplicationContext(), mFindList);
mLv.setAdapter(mMyAdapter);
mBt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(!mEt.getText().toString().trim().equals("")){
mLv.setVisibility(View.VISIBLE);
mFindList.clear();
String key = mEt.getText().toString().trim();
mFindList = findListfromCacheByKey(key);
if(mFindList != null && !key.equals(mMyAdapter.getKey())){
//通知list数据改变了
mMyAdapter.setKey(key);
mMyAdapter.notifyDataSetChanged();
}
}else{
mLv.setVisibility(View.GONE);
}
}
});
}
private void initCacheData() {
// TODO Auto-generated method stub
for(int i = 1; i< 11000 ;i++){
UserInfo mInfo = new UserInfo();
mInfo.setName(i+" 号");
mInfo.setNumber("my number is "+i);
mCacheList.add(mInfo);
}
}
TextWatcher tw = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
if(!mEt.getText().toString().trim().equals("")){
mLv.setVisibility(View.VISIBLE);
mFindList.clear();
String key = mEt.getText().toString().trim();
mFindList = findListfromCacheByKey(key);
if(mFindList != null && !key.equals(mMyAdapter.getKey())){
//通知list数据改变了
mMyAdapter.setKey(key);
mMyAdapter.notifyDataSetChanged();
}
}else{
mLv.setVisibility(View.GONE);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
};
private List<UserInfo> findListfromCacheByKey(String key) {
// TODO Auto-generated method stub
for (int i = 0; i < mCacheList.size(); i++) {
UserInfo userInfo = mCacheList.get(i);
if (userInfo.getName().contains(key) || userInfo.getNumber().contains(key)) {
mFindList.add(userInfo);
}
}
return mFindList;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
UserInfo.java
package com.example.autofindfromlist;
public class UserInfo {
private String name = "";
private String number = "";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
MyAdapter.java
package com.example.autofindfromlist;
import java.util.List;
import android.content.Context;
import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
private List<UserInfo> data;
private LayoutInflater layoutInflater;
private Context context;
private String key;
public MyAdapter(Context context, List<UserInfo> data) {
this.context = context;
this.data = data;
this.key = "";
this.layoutInflater = LayoutInflater.from(this.context);
}
/**
* 组件集合,对应list.xml中的控件
*
* @author Administrator
*/
public final class Zujian {
public TextView tv_name;
public TextView tv_number;
}
@Override
public int getCount() {
return data.size();
}
/**
* 获得某一位置的数据
*/
@Override
public Object getItem(int position) {
return data.get(position);
}
/**
* 获得唯一标识
*/
@Override
public long getItemId(int position) {
return position;
}
public void setKey(String key) {
// TODO Auto-generated method stub
this.key = key;
}
public String getKey() {
return key;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Zujian zujian = null;
if (convertView == null) {
zujian = new Zujian();
// 获得组件,实例化组件
convertView = layoutInflater.inflate(R.layout.item, null);
zujian.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
zujian.tv_number = (TextView) convertView.findViewById(R.id.tv_num);
convertView.setTag(zujian);
} else {
zujian = (Zujian) convertView.getTag();
}
String name = data.get(position).getName();
String number = data.get(position).getNumber();
if (name.contains(getKey())) {
zujian.tv_name.setText("");
zujian.tv_name.append((addStyleSpan(name, name.indexOf(getKey()),
(name.indexOf(getKey()) + getKey().length()))));
} else {
zujian.tv_name.setText(name);
}
if (number.contains(getKey())) {
zujian.tv_number.setText("");
zujian.tv_number.append((addStyleSpan(number,
number.indexOf(getKey()),
(number.indexOf(getKey()) + getKey().length()))));
// data.get(position).getNumber().indexOf(getKey()),
// data.get(position).getNumber()
// .indexOf(getKey() + getKey().length()))));
} else {
zujian.tv_number.setText(number);
}
Log.i("zpf ", " position is ->" + position + " ; key ->" + getKey());
return convertView;
}
private SpannableString addStyleSpan(String str, int start, int end) { //字符串的局部样式的订制 http://www.2cto.com/kf/201409/333384.html
SpannableString spanString = new SpannableString(str);
StyleSpan span = new StyleSpan(Typeface.BOLD);// 加粗
spanString
.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return spanString;
}
}
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="30sp" />
<TextView
android:id="@+id/tv_num"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="30sp" />
</LinearLayout>