android获取系统通讯录

时间:2023-03-09 09:43:58
android获取系统通讯录
 package com.example.administrator.yunphone.View;

 import android.app.Fragment;
import android.database.Cursor;
import android.nfc.Tag;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast; import com.example.administrator.yunphone.R; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* Created by Administrator on 2016/7/13.
*/
public class ContactsFragment extends Fragment {
private View mView;
private List<Map<String, Object>> mList;
private ListView mListview; private String TAG = "log";
private List nameList;
private List phoneList; @Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_contacts_layout, null);
getContactsPhone();
return mView;
}
/*
*获取系统联系人
*/
public void getContactsPhone() {
nameList=new ArrayList();
phoneList=new ArrayList(); Cursor cursor = getActivity().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
int contactIdIndex = 0;
int nameIndex = 0; if (cursor.getCount() > 0) {
contactIdIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID);
nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
}
while (cursor.moveToNext()) {
String contactId = cursor.getString(contactIdIndex);
String name = cursor.getString(nameIndex);
Log.i(TAG, contactId);
Log.i(TAG, name);
nameList.add(name);
/*
* 查找该联系人的phone信息
*/
Cursor phones = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId,
null, null);
int phoneIndex = 0;
if (phones.getCount() > 0) {
phoneIndex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
}
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phoneIndex);
Log.i(TAG, phoneNumber);
phoneList.add(phoneNumber);
} }
//获取到系统联系人之后数据显示
setListView();
} /*
*设置listView显示的内容
*/
private void setListView() {
mListview = (ListView) mView.findViewById(R.id.list);
mList = new ArrayList();
for (int i = 0; i < nameList.size(); i++) {
Map map = new HashMap();
map.put("name", nameList.get(i));
map.put("num", phoneList.get(i));
mList.add(map);
}
SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity(), mList, R.layout.contacts_item, new String[]{"name", "num"}, new int[]{R.id.name, R.id.phone});
mListview.setAdapter(simpleAdapter);
mListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(), "进入联系人详情", Toast.LENGTH_SHORT).show();
}
});
} }