`

获取联系人

 
阅读更多
public class Main extends Activity 
{
	private ListView lvOfContact;
	private List<HashMap<String, String>> mPersonInfos;
	
	@Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        lvOfContact = (ListView)findViewById(R.id.lv_contact);
        
        mPersonInfos = new ArrayList<HashMap<String, String>>();
        Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, 
        		Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");//最后这个参数实现排序
        initContactInfo(cursor);
        SimpleAdapter mSimpleAdapter = new SimpleAdapter(  
                this,   
                mPersonInfos, //数据源  
                R.layout.contact_item, //ListView中显示的每一个元素的布局  
                new String[]{"name", "number"}, //分别对应View中的Id  
                new int[]{R.id.TvDialContactName, R.id.TvNumAddr});  
        lvOfContact.setAdapter(mSimpleAdapter);
    }
	
	/**	生成联系人ArrayList*/
	private void initContactInfo(Cursor cursor)
	{
		if(cursor == null)
		{
			return;
		}
		while(cursor.moveToNext())
		{
			HashMap<String, String> mapOfPerson = new HashMap<String, String>(); 
			mapOfPerson.put("name", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
			mapOfPerson.put("number", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
			mPersonInfos.add(mapOfPerson);
		}
	}
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics