android-ContentResolver

通过适配器去取得联系人名单  并现实电话号码



适配器

package zhang.example;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

public class ContactsAdapter extends CursorAdapter{

	private ContentResolver mContent;
	public ContactsAdapter(Context context, Cursor c) {
		super(context, c);
		// TODO Auto-generated constructor stub
		mContent = context.getContentResolver();
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		// TODO Auto-generated method stub
		//取得通讯录人员的名字
		((TextView)view).setText(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)));
		
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		final LayoutInflater inflater = LayoutInflater.from(context);
		final TextView view=(TextView)inflater.inflate(android.R.layout.simple_dropdown_item_1line, parent,false);
		view.setText(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)));

		return view;
		
	}
	public String converTOString(Cursor cursor){
		return cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
		
	}
	public Cursor runQCursorOnBackgrpundThread(CharSequence constraint){
		if(getFilterQueryProvider()!=null){
			return getFilterQueryProvider().runQuery(constraint);
		}
		StringBuilder buffer=null;
		String[] args=null;
		if(constraint!=null){
			buffer=new StringBuilder();
			buffer.append("UPPER(");
			buffer.append(ContactsContract.Contacts.DISPLAY_NAME);
			buffer.append(")GLOB?");
			args=new String[]{
					constraint.toString().toUpperCase()+"*"
			};
			
		}
		return mContent.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, contentResolver.PEOPLE_PROJECTION, buffer==null?null:buffer.toString(), args, "");
		
	}

}

主要的activity

package zhang.example;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

public class ContactsAdapter extends CursorAdapter{

	private ContentResolver mContent;
	public ContactsAdapter(Context context, Cursor c) {
		super(context, c);
		// TODO Auto-generated constructor stub
		mContent = context.getContentResolver();
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		// TODO Auto-generated method stub
		//取得通讯录人员的名字
		((TextView)view).setText(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)));
		
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		final LayoutInflater inflater = LayoutInflater.from(context);
		final TextView view=(TextView)inflater.inflate(android.R.layout.simple_dropdown_item_1line, parent,false);
		view.setText(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)));

		return view;
		
	}
	public String converTOString(Cursor cursor){
		return cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
		
	}
	public Cursor runQCursorOnBackgrpundThread(CharSequence constraint){
		if(getFilterQueryProvider()!=null){
			return getFilterQueryProvider().runQuery(constraint);
		}
		StringBuilder buffer=null;
		String[] args=null;
		if(constraint!=null){
			buffer=new StringBuilder();
			buffer.append("UPPER(");
			buffer.append(ContactsContract.Contacts.DISPLAY_NAME);
			buffer.append(")GLOB?");
			args=new String[]{
					constraint.toString().toUpperCase()+"*"
			};
			
		}
		return mContent.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, contentResolver.PEOPLE_PROJECTION, buffer==null?null:buffer.toString(), args, "");
		
	}

}

布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    <AutoCompleteTextView
    	android:id="@+id/actv"
    	android:layout_width="fill_parent"
    	android:layout_height="wrap_content"
    	/>
    	<TextView
    		android:id="@+id/myTextView01"
    		android:layout_width="fill_parent"
    		android:layout_height="wrap_content"
    		/>
</LinearLayout>

需要配置权限


 <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值