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();
}
});
} }

最新文章

  1. Entity Framework Code First实体对象变动跟踪
  2. scrollHeight、scrollTop等的比较
  3. 原生js-焦点图轮播
  4. PHP读取XML文件数据
  5. JavaScript基础12——js的方法重载
  6. forfiles命令批量删除N天前文件
  7. Xcode插件开发
  8. 在linux下配置Nginx+Java+PHP的环境
  9. nginx图片过滤处理模块http_image_filter_module安装配置笔记
  10. winform Label与DataGridView右对齐 分类: WinForm 2014-05-19 20:51 446人阅读 评论(0) 收藏
  11. smarty(原理概述)
  12. SubTool 电影字幕下载程序
  13. 基于gdal的格网插值
  14. Python三十个常见的脚本汇总
  15. 常见的SQL调优(SQL Tuning)Tips
  16. springboot linux启动方式
  17. android to hide the keybord
  18. Linux中重命名乱码文件
  19. feign client 的简单使用(1)
  20. 2.3.3 Button(按钮)与ImageButton(图像按钮)

热门文章

  1. C# RFID windows 服务 串口方式
  2. view的setTag() 和 getTag()应用 (转)
  3. 用 C# 实现一个简单的 Rest Service 供外部调用
  4. linux下SVN忽略文件/文件夹的方法
  5. 【SQL Server】数据库是单个用户的 无法顺利进行操作 怎么解决
  6. loadrunner解决在项目中的难点解决
  7. css的重置和原子类的使用
  8. JQuery学习之操作CSS
  9. Ajax实现点击省份显示相应城市
  10. 把spring-boot项目部署到tomcat容器中