通过系统自带的内容提供器(ContentResolver)读取系统的通讯录,并设置点击事件

1、布局

主布局只有一个listview,用来显示电话簿的名字和手机号码
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:tools="http://schemas.android.com/tools"
    4. android:layout_width="match_parent"
    5. android:layout_height="match_parent"
    6. tools:context="lpc.com.project1722.MainActivity">
    7. <ListView
    8. android:id="@+id/list"
    9. android:layout_width="match_parent"
    10. android:layout_height="match_parent">
    11. </ListView>
    12. </RelativeLayout>

布局2

listview的内容布局,只有两个TextView
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <ImageView
  6. android:background="#EE00"
  7. android:layout_width="match_parent"
  8. android:layout_height="1dp" />
  9. <TextView
  10. android:id="@+id/name"
  11. android:textSize="25sp"
  12. android:text="我是名字"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content" />
  15. <TextView
  16. android:id="@+id/number"
  17. android:textSize="20sp"
  18. android:text="我是号码"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content" />
  21. <ImageView
  22. android:background="#EE00"
  23. android:layout_width="match_parent"
  24. android:layout_height="1dp" />
  25. </LinearLayout>


2、java文件

  1. package lpc.com.project1722;
  2. import android.content.Intent;
  3. import android.database.Cursor;
  4. import android.net.Uri;
  5. import android.os.Bundle;
  6. import android.provider.ContactsContract;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.AdapterView;
  12. import android.widget.BaseAdapter;
  13. import android.widget.ListView;
  14. import android.widget.TextView;
  15. import java.util.ArrayList;
  16. public class MainActivity extends AppCompatActivity {
  17. private ListView list;
  18. private ArrayList<String> nameList = new ArrayList<String>();
  19. private ArrayList<String> numberList = new ArrayList<String>();
  20. myAdapter adapter = new myAdapter();
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25. list = (ListView) findViewById(R.id.list);
  26. readContacts();
  27. list.setAdapter(adapter);
  28. list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  29. @Override
  30. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  31. Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ numberList.get(position)));
  32. startActivity(intent);
  33. }
  34. });
  35. }
  36. private void readContacts() {
  37. Cursor cursor = null;
  38. try{
  39. cursor = getContentResolver().query(
  40. ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
  41. null,null,null,"sort_key asc");
  42. //sort_key asc 这个排序是按照数字,A到Z的顺序排序
  43. //last_time_contacted 最后一次的通话时间,此时为根据通话时间排序
  44. while (cursor.moveToNext()){
  45. String displayName = cursor.getString(cursor.getColumnIndex(
  46. ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
  47. nameList.add(displayName);
  48. String number = cursor.getString(cursor.getColumnIndex(
  49. ContactsContract.CommonDataKinds.Phone.NUMBER));
  50. numberList.add(number);
  51. }
  52. }catch (Exception e){
  53. e.printStackTrace();
  54. }finally {
  55. cursor.close();
  56. }
  57. }
  58. class myAdapter extends BaseAdapter{
  59. public myAdapter() {
  60. }
  61. @Override
  62. public int getCount() {
  63. return nameList.size();
  64. }
  65. @Override
  66. public Object getItem(int position) {
  67. return position;
  68. }
  69. @Override
  70. public long getItemId(int position) {
  71. return position;
  72. }
  73. @Override
  74. public View getView(int position, View convertView, ViewGroup parent) {
  75. ViewHolder viewHolder;
  76. View view;
  77. view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.list_item, null);
  78. viewHolder = new ViewHolder();
  79. viewHolder.name = (TextView) view.findViewById(R.id.name);
  80. viewHolder.number = (TextView) view.findViewById(R.id.number);
  81. viewHolder.name.setText(nameList.get(position));
  82. viewHolder.number.setText(numberList.get(position));
  83. return view;
  84. }
  85. class ViewHolder {
  86. public TextView name;
  87. public TextView number;
  88. }
  89. }
  90. }

3、manifest设置

基本上是默认设置,只添加了一个读取 系统联系人的权限

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="lpc.com.project1722">
  4. <uses-permission android:name="android.permission.READ_CONTACTS"/>
  5. <uses-permission android:name="android.permission.CALL_PHONE"/>
  6. <application
  7. android:allowBackup="true"
  8. android:icon="@mipmap/ic_launcher"
  9. android:label="@string/app_name"
  10. android:supportsRtl="true"
  11. android:theme="@style/AppTheme">
  12. <activity android:name=".MainActivity">
  13. <intent-filter>
  14. <action android:name="android.intent.action.MAIN" />
  15. <category android:name="android.intent.category.LAUNCHER" />
  16. </intent-filter>
  17. </activity>
  18. </application>
  19. </manifest>


上一篇:UVA 10273 Eat or Not to Eat?


下一篇:Add and Search Word - Data structure design 解答