AutoCompleteTextView结合自定义适配器

转载地址:http://www.pocketdigi.com/20130124/981.html?utm_source=tuicool

这里没有用CursorAdapter,而是自己继承BaseAdapter写了个适配器.
与ListView不同,AutoCompleteTextView的适配器除了继承BaseAdapter外,还要实现Filterable接口。Filterable接口中有个getFilter方法,用于获取过滤器,我们需要自己写个继承Filter的过滤器,实现数据库查询。
代码使用了androidannotations.

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.ImageView;
import android.widget.TextView;
import cn.jtang.discussion.R;
import cn.jtang.discussion.db.UserDB;
import cn.jtang.discussion.mode.User;
 
import com.googlecode.androidannotations.annotations.Bean;
import com.googlecode.androidannotations.annotations.EBean;
import com.googlecode.androidannotations.annotations.RootContext;
 
@EBean
public class LoginUsernameAdapter extends BaseAdapter implements Filterable {
	LayoutInflater mInflater;
	ArrayList<User> users;
	String key;
	@RootContext
	Context context;
	@Bean
	UserDB userDb;
	DBFilter filter;
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (users != null && users.size() > 0) {
			return users.size();
		}
		return 0;
	}
 
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return users.get(position);
	}
 
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
 
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (mInflater == null) {
			mInflater = LayoutInflater.from(context);
		}
		
		final User user = users.get(position);
 
		View view = mInflater.inflate(R.layout.item_actv_username, null);
		TextView tv_username = (TextView) view.findViewById(R.id.tv_username);
		tv_username.setText(user.getUsername());
		ImageView iv_delete = (ImageView) view.findViewById(R.id.iv_delete);
		//添加点击事件
		iv_delete.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//点击后删除用户
				userDb.deleteUser(user);
				users.remove(position);
				notifyDataSetChanged();
			}
		});
		return view;
	}
	/**
	 * 获取过滤器
	 */
	@Override
	public Filter getFilter() {
		// TODO Auto-generated method stub
		if (filter == null) {
			filter = new DBFilter();
		}
		return filter;
	}
 
	/**
	 * 数据库查询过滤器
	 * 
	 * @author Administrator
	 * 
	 */
	private class DBFilter extends Filter {
		/**
		 * 查询数据库
		 */
		@Override
		protected FilterResults performFiltering(CharSequence prefix) {
			// TODO Auto-generated method stub
			//查询结果保存到FilterResults对象里
			FilterResults results = new FilterResults();
			List<User> queryUsers = userDb.query(prefix.toString());
			results.values = queryUsers;
			results.count = queryUsers.size();
			return results;
		}
 
		/**
		 * 更新UI
		 */
		@Override
		protected void publishResults(CharSequence constraint, FilterResults results) {
			// TODO Auto-generated method stub
			List<User> queryUsers = (List<User>) results.values;
			//把结果读取出复制到users里
			if (users == null) {
				users = new ArrayList<User>();
			}
			if (users.size() > 0) {
				users.clear();
			}
 
			if (queryUsers != null && queryUsers.size() > 0)
				for (User user : queryUsers) {
					users.add(user);
					notifyDataSetChanged();
				}
		}
 
	}
}

© 2013, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接!  应用开发笔记



注意:注意参考Filter的用法!

你可能感兴趣的:(AutoCompleteTextView结合自定义适配器)