使用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. [转载]java的传值和传引用

    本文转载自:https://blog.csdn.net/weixin_36759405/article/details/82764339 基本类型(byte,short,int,long,double ...

  2. LeetCode:175.组合两个表

    题目链接:https://leetcode-cn.com/problems/combine-two-tables/ 题目 表1: Person +-------------+---------+ | ...

  3. JS和JS是IE上JavaScript或JScript的缩写。

    JS和JS是IE上JavaScript或JScript的缩写.javascript是所有浏览器的开放式标准脚本语言JScript是微软自己的开放式脚本语言标准,只有微软的IE浏览器遵循.JScript ...

  4. Java--java.util.stream.Collectors文档实例

    // java.util.stream.Collectors 类的主要作用就是辅助进行各类有用的 reduction 操作,例如转变输出为 Collection,把 Stream 元素进行归组. pu ...

  5. CSS 使用calc()获取当前可视屏幕高度

    来自:https://blog.csdn.net/qq_32063079/article/details/89766442 先了解一下CSS3的相对长度单位(参考详细教程) : 相对长度单位指定了一个 ...

  6. 基于Java使用Flink读取CSV文件,针对批处理,多表联合两种方式Table类和Join方法的实现数据处理,再入CSV文件

    Maven依赖 源头 <dependencies> <dependency> <groupId>org.projectlombok</groupId> ...

  7. 《Linux就该这么学》day1-day2

    ps:原谅我的书法出自鲁迅的<野草> <Linux就该这么学>书本介绍: 本书是由全国多名红帽架构师(RHCA)基于最新Linux系统共同编写的高质量Linux技术自学教程,极 ...

  8. three.js之正投影摄像机与透视投影摄像机的区别

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. C++——INI文件详解

    原创声明:本文系博主原创文章,转载及引用请注明出处. 1. INI文件介绍 INI是英文单词 INItialization 的缩写,常作为Windows系统下的配置文件.INI文件是文本文件,通常用于 ...

  10. Python:多进程。

    参考:https://www.liaoxuefeng.com/wiki/1016959663602400/1017628290184064 Python程序实现多进程(multiprocessing) ...