`
56858752
  • 浏览: 61235 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

Android联系人自动过滤并显示“新建联系人”

 
阅读更多

ROM之家

 

此文写给新手的 高手莫看哈!!!


主要更改是ListView的adapter来实现自动过滤。

Demo结构:


java文件:

ContactActivity.java    主屏幕
NewContactAdapter.java    显示“新建联系人”的adapter
PhoneNumberAdapter.java    显示通讯录得adapter
ContactEntity.java    通讯录实体类

Layout文件:

main.xml    一个editText和一个listView
list_item.xml    自定义listview的item布局
list_item_2.xml    “新建联系人”的item布局

 

主要是下面三个文件(demo已上传 有需要的可以下载)


ContactActivity.java

package com.yalitong.activity;

import java.util.ArrayList;
import java.util.List;

import com.yalitong.adapter.NewContactAdapter;
import com.yalitong.adapter.PhoneNumberAdapter;
import com.yalitong.entity.ContactEntity;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class ContactActivity extends Activity {
//	private String tag = "ContactActivity";
	private Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
	private String[] projection = new String[] {
			ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
			ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
			ContactsContract.CommonDataKinds.Phone.NUMBER };
	private Cursor cursor = null;
	private ListView listView = null;
	private EditText numberEditText = null;
	
	private BaseAdapter baseAdapter = null;
	private List<ContactEntity> list = new ArrayList<ContactEntity>();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		listView = (ListView)findViewById(R.id.list);
		baseAdapter = new PhoneNumberAdapter(this,list);
		listView.setAdapter(baseAdapter);
		
		numberEditText = (EditText)findViewById(R.id.number);
		numberEditText.setOnKeyListener(new NumberTextView());
	}

	@Override
	protected void onResume() {
		super.onResume();
		queryContacts(uri, projection, null, null, null);
	}
	
	/**
	 * 监听键盘事件
	 * @author yangfan
	 *
	 */
	private class NumberTextView implements OnKeyListener{
		String queryNubmer = null;
		String selection = null;
		String[] selectionArgs = null;
		@Override
		public boolean onKey(View v, int keyCode, KeyEvent event) {
		
			numberEditText = (EditText)v;
			queryNubmer = numberEditText.getText().toString().trim();
			//70-83行可以再优化一下,我这里每次事件都会去查询通讯录 这样肯定不是最好的 大家可以自己优化下
			
			//每次点击键盘的时候先清理list中的数据,如果不清除原先的数据,就会导致ListView数据不停地增加
			list.clear();
			//
			selection = ContactsContract.CommonDataKinds.Phone.NUMBER +" like ?";
			selectionArgs = new String[]{queryNubmer + "%"};
			queryContacts(uri, projection, selection, selectionArgs, null);
			if(list.size() == 0){
				//当没有查到数据的时候 显示“新建联系人”字样
				baseAdapter = new NewContactAdapter(ContactActivity.this);
			}else if(list.size() > 0){
				baseAdapter = new PhoneNumberAdapter(ContactActivity.this,list);
			}
			listView.setAdapter(baseAdapter);
			baseAdapter.notifyDataSetChanged();
			return false;
		}
	}
	
	/**
	 * 查询通讯录中的数据 并添加在list中
	 * @param uri
	 * @param projection
	 * @param selection
	 * @param selectionArgs
	 * @param sortOrder
	 */
	private void queryContacts(Uri uri, String[] projection,
            String selection, String[] selectionArgs, String sortOrder){
		cursor = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
		ContactEntity contactEntity = null;
		if(cursor.getCount() > 0){
			while(cursor.moveToNext()){
				contactEntity = new ContactEntity();
				contactEntity.setContactName(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
				contactEntity.setContactNumber(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
				list.add(contactEntity);
			}
		}
	}
	
	
}

 

PhoneNumberAdapter.java

package com.yalitong.adapter;

import java.util.List;
import com.yalitong.activity.R;
import com.yalitong.entity.ContactEntity;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class PhoneNumberAdapter extends BaseAdapter {

//	private String tag = "PhoneNumberAdapter";
	private Context context = null;
	private List<ContactEntity> list = null;
	
	public PhoneNumberAdapter(Context context , List<ContactEntity> list){
		this.context = context;
		this.list = list;
	}
	
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView == null){
			convertView = LayoutInflater.from(context).inflate(R.layout.list_item, null);
			CacheView cacheView = new CacheView();
			cacheView.nameTextView = (TextView)convertView.findViewById(R.id.item_name);
			cacheView.numberTextView = (TextView)convertView.findViewById(R.id.item_number);
			convertView.setTag(cacheView);
		}
		CacheView cacheView = (CacheView)convertView.getTag();
		cacheView.nameTextView.setText(list.get(position).getContactName());
		cacheView.numberTextView.setText(list.get(position).getContactNumber());
		return convertView;
	}
	
	private class CacheView{
		TextView nameTextView = null;
		TextView numberTextView = null;
	}

}

 

 

NewContactAdapter.java

package com.yalitong.adapter;

import com.yalitong.activity.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class NewContactAdapter extends BaseAdapter {

//	private String tag = "NewContactAdapter";
	private Context context = null;
	
	public NewContactAdapter(Context context){
		this.context = context;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return 1;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		//listView中最常见的优化方法
		if(convertView == null){
			convertView = LayoutInflater.from(context).inflate(R.layout.list_item_2, null);
			CacheView cacheView = new CacheView();
			cacheView.nameTextView = (TextView)convertView.findViewById(R.id.newContact);
			convertView.setTag(cacheView);
		}
		
		CacheView cacheView = (CacheView)convertView.getTag();
		cacheView.nameTextView.setText(R.string.newContact);
		return convertView;
	}
	
	private class CacheView{
		TextView nameTextView = null;
	}

}

 

 

 

 

 

 

 

 

  • 大小: 2.4 MB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics