• android的本地通讯录获取以及RecyclerView展示


    在这里插入图片描述
    实体类

    package com.example.mydailer.po;
    
    public class User {
        private String name;
        private String telPhone;
    
        public User(String name, String telPhone) {
            this.name = name;
            this.telPhone = telPhone;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getTelPhone() {
            return telPhone;
        }
    
        public void setTelPhone(String telPhone) {
            this.telPhone = telPhone;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    数据适配器

    package com.example.mydailer;
    
    import android.content.Context;
    
    import androidx.annotation.NonNull;
    import androidx.recyclerview.widget.RecyclerView;
    
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import com.example.mydailer.po.User;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
    
        private static final String TAG="Adapter";
        private String[] names;
        private int[] icons;
        private String[] introduces;
        private List<User> user;
        List<String> contactsList=new ArrayList<String>();
    
    
        public Adapter() {
        }
    
        public Adapter(List<User> user) {
            this.user = user;
        }
    
    
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            ViewHolder holder=new  ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false));
            return holder;
        }
    
        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    
            holder.name.setText(user.get(position).getName());
           // names= user.get(position).getName();
            holder.introduce.setText(user.get(position).getTelPhone());
        }
    
        @Override
        public int getItemCount() {
    
            return user == null ? 0 : user.size();
        }
    
        private  LayoutInflater mlayoutInflater;
        public static class ViewHolder extends RecyclerView.ViewHolder{
            TextView name;
            ImageView iv;
            TextView introduce;
            public ViewHolder(View view){
                super(view);
                name =(TextView) view.findViewById(R.id.name);
                iv=(ImageView) view.findViewById(R.id.iv);
                introduce=(TextView) view.findViewById(R.id.introduce);
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72

    MainActivity

    package com.example.mydailer;
    
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.core.app.ActivityCompat;
    import androidx.core.content.ContextCompat;
    import androidx.recyclerview.widget.LinearLayoutManager;
    import androidx.recyclerview.widget.RecyclerView;
    
    import android.Manifest;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.ContactsContract;
    
    
    import android.util.Log;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    
    import com.example.mydailer.po.User;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class MainActivity extends AppCompatActivity {
    
        private RecyclerView mrecyclerView;
        private Adapter adapter;
         List<User> userList =new ArrayList<>();
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
         mrecyclerView =findViewById(R.id.recyclerView_txl);
         mrecyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
    
    
            //这个地方就是请求权限
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
                    != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(
                        MainActivity.this,new String[]{Manifest.permission.READ_CONTACTS},1);
            } else {
                readContacts();
            }
            //readContacts();
        }
    //读取联系人
        private void readContacts()
        {
            Cursor cursor=null;
            try {
                cursor =getContentResolver().query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,null,null,null);
                while (cursor.moveToNext()) {
                    int i_name=cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
                    String displayName = cursor.getString(i_name);
                    int i_number=cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                    String number = cursor.getString(i_number);
                    userList.add(new User(displayName,number));
    
                }
                Adapter adapter =new Adapter(userList);
                mrecyclerView.setAdapter(adapter);
    
            }catch (Exception e)
            {
                e.printStackTrace();
            }finally {
                if (cursor!=null)
                {
                    cursor.close();
                }
            }
        }
    //    private void tocall() {
    //
    //
    //            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + editTextPhoneString));
    //            startActivity(intent);
    //        }
        }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91

    activity_main

    
    
    <LinearLayout 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=".MainActivity">
    
      <androidx.recyclerview.widget.RecyclerView
    
            android:id="@+id/recyclerView_txl"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
    
            android:layout_centerHorizontal="true"/>
    
    LinearLayout>
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    recycler_item

    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp">
    
        <ImageView
            android:id="@+id/iv"
            android:layout_width="80dp"
            android:layout_height="60dp"
            android:src="@drawable/txr" />
    
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp">
    
            <TextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="通讯人"
                android:textColor="#FF8F03"
                android:textSize="20sp" />
    
            <TextView
                android:id="@+id/introduce"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/name"
                android:layout_marginTop="10dp"
                android:ellipsize="end"
                android:maxLines="2"
                android:text="河南郑州,7月19日"
                android:textColor="#FF716C6D"
                android:textSize="16sp" />
    
    
        RelativeLayout>
    
    LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    AndroidManifest.xml

    
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        package="com.example.mydailer">
        <uses-permission android:name="android.permission.READ_CONTACTS"/>
        <uses-permission android:name="android.permission.CALL_PHONE"/>
        <application
            android:allowBackup="true"
            android:dataExtractionRules="@xml/data_extraction_rules"
            android:fullBackupContent="@xml/backup_rules"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/Theme.MyDailer"
            tools:targetApi="31">
            <activity
                android:name=".MainActivity"
                android:exported="true">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                intent-filter>
            activity>
        application>
    
    manifest>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    记得要添加这两条权限

        <uses-permission android:name="android.permission.READ_CONTACTS"/>
        <uses-permission android:name="android.permission.CALL_PHONE"/>
    
    • 1
    • 2
  • 相关阅读:
    【java】建筑施工一体化智慧工地信息管理系统源码
    DPDK系列之三十四DPDK并行机制的同步控制
    109.firefly-extboot的生成脚本
    超详细 | 萤火虫算法原理及其实现(Matlab)
    不同大小的缓冲区对 MD5 计算速度的影响
    基于SSM的课程管理系统
    JVM学习六
    Hadoop搭建————搭建前的准备
    外汇天眼:德国PPI利淡欧美镑美跌逾百点,美元涨近百点,黄金跌约20美元,关注美制造业指数
    基于JavaSwing开发帐号管理系统+报告 课程设计 大作业源码
  • 原文地址:https://blog.csdn.net/u013574207/article/details/126666786