使用ListView显示手机中联系人的姓名和电话号码

父类布局activity_main.xml,子类布局line.xml(一个文件的单独存放)

运行截图:

(避免泄露信息对部分地方进行了涂鸦O(∩_∩)O!)

程序结构

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.asus.a7gary03"> <!-- 读取通讯录权限 -->
<uses-permission android:name="android.permission.READ_CONTACTS" /> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>

AndroidManifest.xml

package com.example.asus.a7gary03;

import android.support.v7.app.AppCompatActivity;

import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.ListView; import java.util.ArrayList;
import java.util.List; public class MainActivity extends AppCompatActivity { ListView lv;
List<String> list_phone, list_name; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
list_name = new ArrayList<String>();
list_phone = new ArrayList<String>();
Cursor c = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, null);
//获取通讯录的信息
startManagingCursor(c);
int phoneIndex = 0, nameIndex = 0;
if (c.getCount() > 0) {
phoneIndex = c
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
// 获取手机号码的列名
nameIndex = c
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
// 获取用户名的列名
}
while (c.moveToNext()) {
String phone = c.getString(phoneIndex);
// 获取手机号码
list_phone.add(phone);
String name = c.getString(nameIndex);
// 获取用户名
list_name.add(name); } ListViewAdapter adapter = new ListViewAdapter(this, list_name,
list_phone);
lv.setAdapter(adapter);
} }

MainActivity

package com.example.asus.a7gary03;

/**
* Created by ASUS on 2018/5/24.
*/ import java.util.List; import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView; @SuppressLint("InflateParams")
public class ListViewAdapter extends BaseAdapter {
List<String> names, phones;
LayoutInflater inflater; @SuppressWarnings("static-access")
public ListViewAdapter(Context context, List<String> names,
List<String> phones) {
inflater = inflater.from(context);
this.names = names;
this.phones = phones;
} @Override
public int getCount() {
return names.size();
} @Override
public Object getItem(int position) {
return position;
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
view = inflater.inflate(R.layout.item, null);
TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
TextView tv_phone = (TextView) view.findViewById(R.id.tv_number);
tv_name.setText(names.get(position));
tv_phone.setText(phones.get(position));
} else {
view = convertView;
}
return view;
} }

ListViewAdapter

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.asus.a7gary03.MainActivity"> <ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView> </RelativeLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="60sp"
android:orientation="horizontal" > <TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="1111"
android:textSize="20sp" /> <TextView
android:id="@+id/tv_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="2222"
android:textSize="15sp" />
</LinearLayout> </LinearLayout>

item.xml

一、获取手机存储卡权限

    读取通讯录权限 
<uses-permission android:name="android.permission.READ_CONTACTS" />

二、界面布局

activity_main.xml布局中

  TextView中显示手机联系人的的文本框,ListView列出手机中的联系人

item.xml布局中

  两个TextView,一个显示联系人的姓名,一个显示联系人的电话号码

三、实现程序功能

1、获取手机电话号码和联系人信息

        Cursor c = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, null);
//获取通讯录的信息
startManagingCursor(c);
int phoneIndex = 0, nameIndex = 0;
if (c.getCount() > 0) {
phoneIndex = c
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
// 获取手机号码的列名
nameIndex = c
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
// 获取用户名的列名
}
while (c.moveToNext()) {
String phone = c.getString(phoneIndex);
// 获取手机号码
list_phone.add(phone);
String name = c.getString(nameIndex);
// 获取用户名
list_name.add(name); }
Cursor:Android使用的数据库是SQLite数据库,对于数据库记录的操作,可以使用Cursor来进行  (传送门)

getColumnIndex(String columnName)  -------->返回指定列的名称 ,如果不存在返回-1

Cursor c = getContentResolver.query(uri , String[ ] , where , String[ ] , sort);

这条语句相信大家一定经常看到用到,查看sdk帮助文档也很容易找到其中五个参数的意思

第一个参数:是一个URI,指向需要查询的表;

第二个参数:需要查询的列名,是一个数组,可以返回多个列;

第三个参数:需要查询的行,where表示需要满足的查询条件,where语句里面可以有?号;

第四个参数:是一个数组,用来替代上面where语句里面的问号;

第五个参数:表示排序方式;

2、添加BaseAdapter适配器

simpleAdapter的扩展性最好,可以自定义各种各样的布局出来,可以放上ImageView(图片),还可以放上Button(按钮),CheckBox(复选框)等等
 
ListView和Adapter数据适配器的简单介绍:传送门
 
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
view = inflater.inflate(R.layout.item, null);
TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
TextView tv_phone = (TextView) view.findViewById(R.id.tv_number);
tv_name.setText(names.get(position));
tv_phone.setText(phones.get(position));
} else {
view = convertView;
}
return view;
}

LayoutInflater类的作用类似于findViewById()

不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化

而findViewById()是找xml布局文件下的具体widget控件(如Button、TextView等)。

Android_(控件)使用ListView显示Android系统中联系人信息的更多相关文章

  1. Android_(控件)使用ListView显示Android系统中SD卡的文件列表

    使用ListView显示Android SD卡中的文件列表 父类布局activity_main.xml,子类布局line.xml(一个文件的单独存放) 运行截图: 程序结构: <?xml ver ...

  2. Android_(控件)使用ListView显示Android系统SD卡的文件列表_02

    使用ListView显示Android SD卡中的文件列表 父类布局activity_main.xml,子类布局item_filelayout(一个文件的单独存放) 运行截图: 程序结构 <?x ...

  3. 【实用篇】获取Android通讯录中联系人信息

    第一步,在Main.xml布局文件中声明一个Button控件,布局文件代码如下: <LinearLayout xmlns:android="http://schemas.android ...

  4. 查看Android系统中硬件信息的文件

    文件目录: 使用Linux命令,进入到/proc目录 进入/proc目录,可以查看内存信息(memoinfo)或CPU信息(cpuinfo),使用cat命令

  5. Android的WebView控件载入网页显示速度慢的究极解决方案

    Android的WebView控件载入网页显示速度慢的究极解决方案 [转载来源自http://hi.baidu.com/goldchocobo/] 秒(甚至更多)时间才会显示出来.研究了很久,搜遍了国 ...

  6. XE6 FMX之控件绘制与显示

    中午,有个货随手买的2块钱的彩票,尼玛中了540块,这是啥子狗屎气运.稍微吐槽一下,现在开始正规的笔记录入.经常有朋友说为毛我的博客不更新了或者说更新的少了,为啥呢!一来自己懒了,没学习什么新的东西, ...

  7. 与众不同 windows phone (52) - 8.1 新增控件: AutoSuggestBox, ListView, GridView, SemanticZoom

    [源码下载] 与众不同 windows phone (52) - 8.1 新增控件: AutoSuggestBox, ListView, GridView, SemanticZoom 作者:webab ...

  8. 使用shape来定义控件的一些显示属性

    Android中常常使用shape来定义控件的一些显示属性,今天看了一些shape的使用,对shape有了大体的了解,稍作总结 先看下面的代码: <shape> <!-- 实心 -- ...

  9. winform利用ImageList控件和ListView控件组合制作图片文件浏览器

    winform利用ImageList控件和ListView控件组合制作图片文件浏览器,见图,比较简单,实现LISTVIEW显示文件夹图片功能. 1.选择文件夹功能代码: folderBrowserDi ...

随机推荐

  1. asp.net 5.图片和验证码

    1.基本画图 //给用户创建一张图片,并且保持一张图片. //创建一个画布 , )) { //绘画布创建一个画笔 using (Graphics g = Graphics.FromImage(map) ...

  2. sql循环(WITH AS短语也叫做子查询部分)

    --表结构 SELECT id,position,Parentid FROM op_client_sales_structure WITH TEST_CTE AS ( SELECT id,positi ...

  3. squoosh

    谷歌在线压缩图片

  4. 小白学 Python 爬虫(25):爬取股票信息

    人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...

  5. redis的keys常用操作及redis的特性

    redis的keys常用操作 1.获得所有的keys: keys * 2.可以模糊查询 keys:keys  my* 3.删除keys:del mymkey1 mykey2 4.是否存在keys:ex ...

  6. canva绘制圆角矩形

    在做组态的时候,需要支持矩形圆角格式,但是因为canvas本身不带有圆角矩形,需要自行算出坐标进行绘制 方案一.统一圆角 <!DOCTYPE html> <html> < ...

  7. Html-自适应

    自适应 使网页能适应不同终端设备的技术.原理是通过检测视口分辨率来判断是什么终端的,PC,手机还是平板. 做自适应的网页时,需要在代码中加入“祖传代码”,即通用代码. 这是在头部head引入的: &l ...

  8. Java注解【四、自定义注解】

    语法要求.元注解 元注解: Target-适用范围: Retention-类型:源码注解.编译时注解.运行时注解 Inherited-可继承(只能继承类上的注解,接口.类中的方法都不行) Docume ...

  9. 深入学习Mybatis框架(一)- 入门

    1.什么是Mybatis? Mybatis是一个优秀持久层框架,提供了对数据库的一系列操作(增删改查).Mybatis可以避免重复的写JDBC代码,让我们以较少的代码实现对数据库的操作,从而提高开发效 ...

  10. 用Python+Aria2写一个自动选择最优下载方式的E站爬虫

    前言 E站爬虫在网上已经有很多了,但多数都只能以图片为单位下载,且偶尔会遇到图片加载失败的情况:熟悉E站的朋友们应该知道,E站许多资源都是有提供BT种子的,而且通常打包的是比默认看图模式更高清的文件: ...