获取手机联系人的方法:
public static List<ConInfo> getCon(Context context){ ContentResolver resolver = context.getContentResolver(); // 获取手机联系人 Cursor phoneCursor = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, null, null, null); List<ConInfo> list = new ArrayList<ConInfo>(); if (phoneCursor != null) { while (phoneCursor.moveToNext()){ String name = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String num = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); ConInfo conInfo = new ConInfo(name,num); list.add(conInfo); } } return list; }其中ConInfo是一个封装联系人信息的类
private String name; private String num; ConInfo(String name,String num){ setName(name); setNum(num); }ListView中的设置:
public View getView(int i, View view, ViewGroup viewGroup) { //优化:减少view的加载,避免卡顿情况 Viewholder hold = new Viewholder(); if(view == null){ view = View.inflate(getApplicationContext(),R.layout.contact,null); hold.name = (TextView) view.findViewById(R.id.name); hold.num = (TextView) view.findViewById(R.id.num); hold.name.setText(lists.get(i).getName()); hold.num.setText(lists.get(i).getNum()); view.setTag(hold);}else { hold = (Viewholder) view.getTag(); hold.name.setText(lists.get(i).getName()); hold.num.setText(lists.get(i).getNum()); } return view; }其中使用了Viewholder进行了一点优化,减少了view的价值,避免卡顿现象
private static class Viewholder { TextView name; TextView num; }之后可以改成一个完整的通讯录,可以打电话、存多个电话,加入照片