zoukankan      html  css  js  c++  java
  • android获取系统通讯录

      1 package com.example.administrator.yunphone.View;
      2 
      3 import android.app.Fragment;
      4 import android.database.Cursor;
      5 import android.nfc.Tag;
      6 import android.os.Bundle;
      7 import android.provider.ContactsContract;
      8 import android.support.annotation.Nullable;
      9 import android.util.Log;
     10 import android.view.LayoutInflater;
     11 import android.view.View;
     12 import android.view.ViewGroup;
     13 import android.widget.AdapterView;
     14 import android.widget.ListView;
     15 import android.widget.SimpleAdapter;
     16 import android.widget.Toast;
     17 
     18 import com.example.administrator.yunphone.R;
     19 
     20 import java.util.ArrayList;
     21 import java.util.HashMap;
     22 import java.util.List;
     23 import java.util.Map;
     24 
     25 /**
     26  * Created by Administrator on 2016/7/13.
     27  */
     28 public class ContactsFragment extends Fragment {
     29     private View mView;
     30     private List<Map<String, Object>> mList;
     31     private ListView mListview;
     32 
     33     private String TAG = "log";
     34     private List nameList;
     35     private List phoneList;
     36 
     37 
     38     @Nullable
     39     @Override
     40     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     41         mView = inflater.inflate(R.layout.fragment_contacts_layout, null);
     42         getContactsPhone();
     43         return mView;
     44     }
     45     /*
     46      *获取系统联系人
     47      */
     48     public void getContactsPhone() {
     49         nameList=new ArrayList();
     50         phoneList=new ArrayList();
     51 
     52         Cursor cursor = getActivity().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
     53                 null, null, null, null);
     54         int contactIdIndex = 0;
     55         int nameIndex = 0;
     56 
     57         if (cursor.getCount() > 0) {
     58             contactIdIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID);
     59             nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
     60         }
     61         while (cursor.moveToNext()) {
     62             String contactId = cursor.getString(contactIdIndex);
     63             String name = cursor.getString(nameIndex);
     64             Log.i(TAG, contactId);
     65             Log.i(TAG, name);
     66             nameList.add(name);
     67             /*
     68              * 查找该联系人的phone信息
     69              */
     70             Cursor phones = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
     71                     null,
     72                     ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId,
     73                     null, null);
     74             int phoneIndex = 0;
     75             if (phones.getCount() > 0) {
     76                 phoneIndex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
     77             }
     78             while (phones.moveToNext()) {
     79                 String phoneNumber = phones.getString(phoneIndex);
     80                 Log.i(TAG, phoneNumber);
     81                 phoneList.add(phoneNumber);
     82             }
     83 
     84         }
     85         //获取到系统联系人之后数据显示
     86         setListView();
     87     }
     88 
     89     /*
     90     *设置listView显示的内容
     91     */
     92     private void setListView() {
     93         mListview = (ListView) mView.findViewById(R.id.list);
     94         mList = new ArrayList();
     95         for (int i = 0; i < nameList.size(); i++) {
     96             Map map = new HashMap();
     97             map.put("name", nameList.get(i));
     98             map.put("num", phoneList.get(i));
     99             mList.add(map);
    100         }
    101         SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity(), mList, R.layout.contacts_item, new String[]{"name", "num"}, new int[]{R.id.name, R.id.phone});
    102         mListview.setAdapter(simpleAdapter);
    103         mListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    104             @Override
    105             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    106                 Toast.makeText(getActivity(), "进入联系人详情", Toast.LENGTH_SHORT).show();
    107             }
    108         });
    109     }
    110 
    111 }
  • 相关阅读:
    [PY3]——logging
    [PY3]——对iterator的处理(解析式、map、reduce、filter)
    php基础语法(文件加载和错误)
    php基础语法(控制语句、数组、函数)
    php基础语法(数据类型、运算符)
    php基础语法(变量)
    java基础语法
    ztree 获取根节点
    每天一个linux命令
    浅谈Web自适应
  • 原文地址:https://www.cnblogs.com/yoyohong/p/5690799.html
Copyright © 2011-2022 走看看