实体类
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;
}
}
数据适配器
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);
}
}
}
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);
// }
}
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>
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>
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>
记得要添加这两条权限
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>