Android_(控件)使用ListView显示Android系统中联系人信息

使用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等)。

原文地址:https://www.cnblogs.com/1138720556Gary/p/9086162.html

时间: 2024-08-04 17:49:38

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

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

使用ListView显示Android SD卡中的文件列表 父类布局activity_main.xml,子类布局item_filelayout(一个文件的单独存放) 运行截图: 程序结构 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="

Android中使用shape来定义控件的一些显示属性

本人在美工方面一直是比较白痴的,对于一些颜色什么乱七八糟的非常头痛,但是在Android编程中这又是经常涉及到的东西,没办法,只有硬着头皮上. Android中常常使用shape来定义控件的一些显示属性,今天看了一些shape的使用,对shape有了大体的了解,稍作总结: 先看下面的代码:        <shape>            <!-- 实心 -->            <solid android:color="#ff9d77"/>

Android控件之TextView(显示文本框控件)

一.TextView控件的常用属性 android:id——控件的id   android:layout_width——控件的宽度  android:layout_height——控件的高度 android:text——文本内容 android:textSize——文本大小 android:textColor——文本颜色 android:background——控件背景  android:singleLine——是否单行显示(true为单行,false(默认)自动换行) android:text

Android 控件的一些属性--持续更新中...

归纳一些冷门又可能用到的Android控件属性 1.ListView android:drawSelectorOnTop="true" 点击某一条记录,颜色会显示在最上面,记录上的文字被遮住,所以点击文字不放,文字就看不到 android:drawSelectorOnTop="false" 点击某条记录不放,颜色会在记录的后面,成为背景色,但是记录内容的文字是可见的 取消分割线/分隔线 android:divider="@null" listvi

XE6 FMX之控件绘制与显示

FMX是一套UI类库,就相当于以前的VCL,但是相比VCL来说,支持了跨平台,同时也直接内部支持了各种特效动画甚至3D的效果,如果效率性能上来了,这个类库还是很有前景的.这次我主要学习的就是一个FMX窗体是如何绘制并显示出来的,相比较于VCL,有哪些不同之处,以及一个FMX程序的启动运转的最简单剖析.至于各种特效,动画,以及3D等,以后再慢慢的去啃食,贪多嚼不烂. 新建一个FireMonkey的HD Desktop Application,IDE会自动建立一个工程,进入工程,可以发现FMX的程序

MFC9.0 Outlook控件的标题显示无法修改

这是我在开发中遇到的问题,现记录下来,以便帮助你们. 不想看废话的可以只看最后三行,但你会错过很多. 俗话说的好啊,"Wise men learn by other men's mistakes; fools by their own." -------------------------------------------分割线首次登场--------------------------------------------- 可能有的童鞋英语不太好,看不懂上面的东东,好吧我活跃下气氛

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

Android中常常使用shape来定义控件的一些显示属性,今天看了一些shape的使用,对shape有了大体的了解,稍作总结 先看下面的代码: <shape> <!-- 实心 --> <solid android:color="#ff9d77"/> <!-- 渐变 --> <gradient android:startColor="#ff8c00" android:endColor="#FFFFFF&

windows窗体控件之listview列表视图

1.添加标题 winform.listview.gridLines=true;//显示列表线,也可在属性表设置 winform.listview.insertColumn("列标题",列宽,位置,样式) winform.listview.insertColumn("标题2",列宽,位置,样式)//后加的在前面(若不注明位置) winform.listview.insertColumn("第一列", 40, 1); winform.listview

如何:使用TreeView控件实现树结构显示及快速查询

本文主要讲述如何通过使用TreeView控件来实现树结构的显示,以及树节点的快速查找功能.并针对通用树结构的数据结构存储进行一定的分析和设计.通过文本能够了解如何存储层次结构的数据库设计,如何快速使用TreeView控件生产树,以及如何快速查找树节点. 关键词:C# TreeView.树结构存储.树节点查找.层次结构 一.      概述: 树结构(层次结构)在项目的使用中特别常见,在不同项目中使用的控件可能不同(如:在Extjs中使用的是TreePanel控件,WinForm中可能用的是Tre