AnGuoK 2018-01-11 10:38 采纳率: 20%
浏览 1420
已采纳

关于android获取手机联系人信息的问题,求大神帮帮忙?谢谢。

下面是我写的代码,最后真机运行布局上只出现了一条数据。
1、实体类
public class ContactsBean {
private int contactId;
private int icon;
private String contactsName;
private String company;
private String phoneNumber;
private String remark;
private String job;
private String email;

public String getJob() {
    return job;
}

public void setJob(String job) {
    this.job = job;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}



public int getContactId() {
    return contactId;
}

public void setContactId(int contactId) {
    this.contactId = contactId;
}

public int getIcon() {
    return icon;
}

public void setIcon(int icon) {
    this.icon = icon;
}

public String getContactsName() {
    return contactsName;
}

public void setContactsName(String contactsName) {
    this.contactsName = contactsName;
}

public String getCompany() {
    return company;
}

public void setCompany(String company) {
    this.company = company;
}

public String getPhoneNumber() {
    return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
}

public String getRemark() {
    return remark;
}

public void setRemark(String remark) {
    this.remark = remark;
}
    2


    2、主代码

    package com.kag.activity;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

import com.addresslist.R;

import java.security.Provider;
import java.util.ArrayList;
import java.util.List;

import FragmentAdapter.AddContactFromAddressAdapter;
import bean.ContactsBean;

public class AddContactFromAddressList extends AppCompatActivity {
ContactsBean contactsBean;
private ListView lv;
ArrayList contacts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_contact_from_address_list);
lv=findViewById(R.id.lv_contacts_list);
contactsBean=new ContactsBean();
contacts=new ArrayList();
reaContacts(contacts);
AddContactFromAddressAdapter adapter=new AddContactFromAddressAdapter(this,contacts);
lv.setAdapter(adapter);
}
//从手机通讯录中读取数据
public void reaContacts(ArrayList contacts){

    Uri uri=Uri.parse("content://com.android.contacts/contacts");
    ContentResolver resolver=this.getContentResolver();
        Cursor cursor=resolver.query(uri,null,null,null,ContactsContract.Contacts.SORT_KEY_PRIMARY);
    while (cursor.moveToNext()) {
        int  id = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

        String sort_key = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.SORT_KEY_PRIMARY));
        contactsBean.setContactsName(name);
        contactsBean.setContactId(id);



        Cursor phone = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+id, null, null);
        ArrayList phoneNumber=new ArrayList();
        while (phone.moveToNext()) {
            String number = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            phoneNumber.add(number);

            System.out.println(name+":"+number);



        }
        contactsBean.setPhoneNumber(String.valueOf(phoneNumber));
        contacts.add(contactsBean);
        System.out.println(contactsBean.getContactsName()+":"+contactsBean.getPhoneNumber());

    }



}

}

3、适配器
public class AddContactFromAddressAdapter extends BaseAdapter {
private List contacts;
LayoutInflater inflater;
public AddContactFromAddressAdapter(Context context,List contacts){
this.contacts=contacts;
inflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return contacts.size();
}

@Override
public Object getItem(int position) {
    return contacts.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder=null;
    if (convertView==null){
         convertView=inflater.inflate(R.layout.contacts_cell,null);
         holder=new ViewHolder();
        holder.sort_key=convertView.findViewById(R.id.tv_sort_key);
        holder.mPhones=convertView.findViewById(R.id.tv_phones);
        convertView.setTag(holder);
    }else {
        holder= (ViewHolder) convertView.getTag();
    }

    holder.sort_key.setText(contacts.get(position).getContactsName());
    holder.mPhones.setText(contacts.get(position).getPhoneNumber());
    return convertView;
}
private final class ViewHolder{
     public   TextView sort_key;
     public   TextView mPhones;
}

}

4运行结果

图片说明

  • 写回答

3条回答 默认 最新

  • Jayuan19 2018-01-11 12:00
    关注

    在set数据的时候重新清空创建Bean对象就行了 如下
    在while (cursor.moveToNext()) {}
    里面加入
    contactsBean=null;
    contactsBean=new ContactsBean();就行了
    图片说明

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Docker容器里已经安装了ssh,但打包迁移到新机器一直容器一直提示unrecognized service。
  • ¥15 综合布线实例设计,就好看好看不恐怖可好滤镜好聚
  • ¥15 使用moviepy库视频合并时出错
  • ¥30 FLUENT液固传质UDF
  • ¥15 怎么看梯度直方图以,怎么判断梯度消失/爆炸,怎么解决
  • ¥15 aspnetdll文件访问拒绝
  • ¥15 wpf中在模版中寻找元素
  • ¥15 MFC平台生成指定圆
  • ¥15 jmeter出现403
  • ¥500 求华为P30PRO手机硬盘数据恢复