android 获取通讯录并显示listview,算法题+JVM

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

/** 联系人名称 **/

private ArrayList mContacts = new ArrayList();

private static final String[] PHONES_PROJECTION = new String[] {

Phone.DISPLAY_NAME, Phone.NUMBER, Phone.PHOTO_ID, Phone.CONTACT_ID };

/** 联系人显示名称 **/

private static final int PHONES_DISPLAY_NAME_INDEX = 0;

/** 电话号码 **/

private static final int PHONES_NUMBER_INDEX = 1;

/** 头像ID **/

private static final int PHONES_PHOTO_ID_INDEX = 2;

/** 联系人的ID **/

private static final int PHONES_CONTACT_ID_INDEX = 3;

private void getPhoneContacts() {

ContentResolver resolver = getContentResolver();

try {

// 获取手机联系人

Cursor phoneCursor = resolver.que

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

ry(Phone.CONTENT_URI,

PHONES_PROJECTION, null, null, null);

if (phoneCursor != null) {

while (phoneCursor.moveToNext()) {

// 得到手机号码

String phoneNumber = phoneCursor

.getString(PHONES_NUMBER_INDEX);

// 当手机号码为空的或者为空字段 跳过当前循环

if (TextUtils.isEmpty(phoneNumber))

continue;

// 得到联系人名称

String contactName = phoneCursor

.getString(PHONES_DISPLAY_NAME_INDEX);

// 得到联系人ID

Long contactid = phoneCursor

.getLong(PHONES_CONTACT_ID_INDEX);

// 得到联系人头像ID

Long photoid = phoneCursor.getLong(PHONES_PHOTO_ID_INDEX);

// 得到联系人头像Bitamp

Bitmap contactPhoto = null;

// photoid 大于0 表示联系人有头像 如果没有给此人设置头像则给他一个默认的

if (photoid > 0) {

Uri uri = ContentUris.withAppendedId(

ContactsContract.Contacts.CONTENT_URI,

contactid);

InputStream input = ContactsContract.Contacts

.openContactPhotoInputStream(resolver, uri);

contactPhoto = BitmapFactory.decodeStream(input);

} else {

contactPhoto = BitmapFactory.decodeResource(

getResources(), R.drawable.ic_launcher);

}

ContactEntity mContact = new ContactEntity(contactName,

phoneNumber, contactPhoto);

mContacts.add(mContact);

}

phoneCursor.close();

}

} catch (Exception e) {

e.printStackTrace();

}

}

private void initList() {

ListView lv = (ListView) findViewById(R.id.listView1);

lv.setAdapter(new MyAdapter());

}

class MyAdapter extends BaseAdapter {

@Override

public int getCount() {

if (mContacts != null && mContacts.size() > 0) {

return mContacts.size();

} else {

return 0;

}

}

@Override

public Object getItem(int position) {

if (mContacts != null && mContacts.size() > 0) {

return mContacts.get(position);

} else {

return null;

}

}

@Override

public long getItemId(int arg0) {

// TODO Auto-generated method stub

return arg0;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder = null;

if (convertView == null) {

holder = new ViewHolder();

convertView = LayoutInflater.from(MainActivity.this).inflate(

R.layout.item_contact, null);

holder.name = (TextView) convertView.findViewById(R.id.tv_name);

holder.number = (TextView) convertView

.findViewById(R.id.tv_number);

holder.photo = (ImageView) convertView

.findViewById(R.id.iv_photo);

convertView.setTag(holder);

}else{

holder = (ViewHolder) convertView.getTag();

}

ContactEntity contact = mContacts.get(position);

holder.name.setText(contact.getName()+"");

holder.number.setText(contact.getNumber()+"");

holder.photo.setImageBitmap(contact.getPhoto());

return convertView;

}

class ViewHolder {

TextView name;

TextView number;

ImageView photo;

}

}

}

联系人实体类ContactEntity:


package com.yqy.yqy_testtxl;

import android.graphics.Bitmap;

/**

  • 联系人信息

  • @author YQY

*/

public class ContactEntity {

/** 联系人名称 **/

private String name;

/** 联系人号码 **/

private String number;

/** 联系人头像 **/

private Bitmap photo;

@Override

public String toString() {

return “ContactEntity [name=” + name + “, number=” + number

  • “, photo=” + photo + “]”;

}

public ContactEntity(String name, String number, Bitmap photo) {

super();

this.name = name;

this.number = number;

this.photo = photo;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getNumber() {

上一篇:宝藏又小众的体育 运动cc0高清摄影图片素材分享


下一篇:bp神经网络---MATLAB