2 kuanganguo KuangAnGuo 于 2018.01.11 18:38 提问

关于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个回答

JunJ19
JunJ19   2018.01.11 20:00
已采纳

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

sinat_38239454
sinat_38239454   2018.01.11 20:21

..... 没写完吧

g312678
g312678   2018.01.12 09:28

出现这种情况,应该是contactsBean 没有在 while (cursor.moveToNext()) 这个循环里面进行 实例化,每次循环都进行实例化,才能够保证每次的javabean 数据不一样。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!