首页 > 其他 > 详细

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

时间:2016-01-14 12:22:20      阅读:196      评论:0      收藏:0      [点我收藏+]

特别说明:此应用有个BUG,滑动和点击监听 会冲突,等水平提高了,再回头搞

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.project722.MainActivity">
  7. <ListView
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:id="@+id/list" />
  11. </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. <TextView
  6. android:id="@+id/name"
  7. android:text="我是一个名字"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content" />
  10. <TextView
  11. android:id="@+id/number"
  12. android:text="我是一个号码"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content" />
  15. </LinearLayout>

2、java文件

  1. package lpc.com.project722;
  2. import android.content.Context;
  3. import android.database.Cursor;
  4. import android.os.Bundle;
  5. import android.provider.ContactsContract;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.view.LayoutInflater;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import android.widget.AdapterView;
  11. import android.widget.BaseAdapter;
  12. import android.widget.ListView;
  13. import android.widget.TextView;
  14. import android.widget.Toast;
  15. import java.util.ArrayList;
  16. public class MainActivity extends AppCompatActivity {
  17. private ArrayList<String> nameList = new ArrayList<String>();
  18. private ArrayList<String> numberList = new ArrayList<String>();
  19. Context mContext = null;
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_main);
  24. ListView list = (ListView) findViewById(R.id.list);
  25. MyListAdapter Adapter= new MyListAdapter(this);
  26. list.setAdapter(Adapter);
  27. readData();
  28. list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  29. @Override
  30. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  31. Toast.makeText(MainActivity.this,"你点击的是"+ nameList.get(position),
  32. Toast.LENGTH_SHORT).show();
  33. }
  34. });
  35. }
  36. private void readData() {
  37. Cursor cursor = null;
  38. try{
  39. cursor = getContentResolver().query(
  40. ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
  41. null,
  42. null,
  43. null,
  44. null);
  45. while (cursor.moveToNext()){
  46. String displayName = cursor.getString(cursor.getColumnIndex(
  47. ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
  48. ));
  49. nameList.add(displayName);
  50. String number = cursor.getString(cursor.getColumnIndex(
  51. ContactsContract.CommonDataKinds.Phone.NUMBER
  52. ));
  53. numberList.add(number);
  54. }
  55. }catch (Exception e){
  56. e.printStackTrace();
  57. }finally {
  58. cursor.close();
  59. }
  60. }
  61. class MyListAdapter extends BaseAdapter {
  62. public MyListAdapter(Context context) {
  63. mContext = context;
  64. }
  65. public int getCount() {
  66. //设置绘制数量
  67. return nameList.size();
  68. }
  69. public Object getItem(int position) {
  70. return position;
  71. }
  72. public long getItemId(int position) {
  73. return position;
  74. }
  75. public View getView(int position, View convertView, ViewGroup parent) {
  76. TextView title = null;
  77. TextView text = null;
  78. if (convertView == null) {
  79. convertView = LayoutInflater.from(mContext).inflate(
  80. R.layout.list_item, null);
  81. title = (TextView) convertView.findViewById(R.id.name);
  82. text = (TextView) convertView.findViewById(R.id.number);
  83. }
  84. //绘制联系人名称
  85. title.setText(nameList.get(position));
  86. //绘制联系人号码
  87. text.setText(numberList.get(position));
  88. return convertView;
  89. }
  90. }
  91. }

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.project722">
  4. <uses-permission android:name="android.permission.READ_CONTACTS"/>
  5. <application
  6. android:allowBackup="true"
  7. android:icon="@mipmap/ic_launcher"
  8. android:label="@string/app_name"
  9. android:supportsRtl="true"
  10. android:theme="@style/AppTheme">
  11. <activity android:name=".MainActivity">
  12. <intent-filter>
  13. <action android:name="android.intent.action.MAIN" />
  14. <category android:name="android.intent.category.LAUNCHER" />
  15. </intent-filter>
  16. </activity>
  17. </application>
  18. </manifest>





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

原文:http://www.cnblogs.com/liupengcheng/p/5129687.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!