下面是我写的代码,最后真机运行布局上只出现了一条数据。
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运行结果