Android中获取联系人信息(一)

本文介绍了在Android中如何获取联系人信息,通过一个BaseAdapter子类和ViewHolder解决ListView滚动时联系人信息错乱的问题。文章参考了相关资料并提供了可运行的代码示例。
摘要由CSDN通过智能技术生成


        在上一篇学习ListView的时候,看到在API中获取联系人的例子已经不被官方推荐了。于是,手痒又查了查,看看使用Android如何获取联系人信息。这篇可以说着对ListView继续深入学习,也是对android中访问联系人信息的学习。

        在学习的过程中参考了很多资料,其中写得比较好的是这一篇http://xys289187120.blog.51cto.com/3361352/656766

我的例子也基本上是模仿他写的。

       

下面看代码

          
package com.example.contactlist;

import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MainActivity extends ListActivity {

	Context myContext = null;
	MyListAdapter myListAdapter = null;
	TextView name = null;
	TextView number = null;
	
	//这两个List用于暂时存储联系人的名字和号码
	List<String> myContactName = new ArrayList<String>();
	List<String> myContactNumber = new ArrayList<String>();	

	public void onCreate(Bundle savedInstanceState) {
		
		myContext = this;
		
		//使用getContentResolver方法来读取联系人的表
		Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); 
				  
		while(cursor.moveToNext()){ 
				//联系人的ID
				String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
				//联系人的名称
				String name 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值