Android查看手机通讯录(ListView)

深碍√TFBOYSˉ_ 2022-08-21 14:53 274阅读 0赞

一、添加权限:

  1. <uses-permission android:name="android.permission.READ_CONTACTS" />
  2. <!-- 读取通讯录权限 -->

二、item.xml(创建ListView):

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6. <LinearLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="60sp"
  9. android:orientation="horizontal" >
  10. <TextView
  11. android:id="@+id/tv_name"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:layout_gravity="center_vertical"
  15. android:text="1111"
  16. android:textSize="35sp" />
  17. <TextView
  18. android:id="@+id/tv_number"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:layout_gravity="bottom"
  22. android:text="2222"
  23. android:textSize="25sp" />
  24. </LinearLayout>
  25. </LinearLayout>

三、activity_main.xml:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent" >
  5. <ListView
  6. android:id="@+id/lv"
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent" >
  9. </ListView>
  10. </RelativeLayout>

四、ListViewAdapter.java(ListView的适配器):

  1. package com.example.listviewshowlianxipeople;
  2. import java.util.List;
  3. import android.annotation.SuppressLint;
  4. import android.content.Context;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.BaseAdapter;
  9. import android.widget.TextView;
  10. @SuppressLint("InflateParams")
  11. public class ListViewAdapter extends BaseAdapter {
  12. List<String> names, phones;
  13. LayoutInflater inflater;
  14. @SuppressWarnings("static-access")
  15. public ListViewAdapter(Context context, List<String> names,
  16. List<String> phones) {
  17. inflater = inflater.from(context);
  18. this.names = names;
  19. this.phones = phones;
  20. }
  21. @Override
  22. public int getCount() {
  23. return names.size();
  24. }
  25. @Override
  26. public Object getItem(int position) {
  27. return position;
  28. }
  29. @Override
  30. public long getItemId(int position) {
  31. return position;
  32. }
  33. @Override
  34. public View getView(int position, View convertView, ViewGroup parent) {
  35. View view;
  36. if (convertView == null) {
  37. view = inflater.inflate(R.layout.item, null);
  38. TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
  39. TextView tv_phone = (TextView) view.findViewById(R.id.tv_number);
  40. tv_name.setText(names.get(position));
  41. tv_phone.setText(phones.get(position));
  42. } else {
  43. view = convertView;
  44. }
  45. return view;
  46. }
  47. }

五、MainActivity.java:

  1. package com.example.listviewshowlianxipeople;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.app.Activity;
  5. import android.database.Cursor;
  6. import android.os.Bundle;
  7. import android.provider.ContactsContract;
  8. import android.widget.ListView;
  9. public class MainActivity extends Activity {
  10. ListView lv;
  11. List<String> list_phone, list_name;
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16. lv = (ListView) findViewById(R.id.lv);
  17. list_name = new ArrayList<String>();
  18. list_phone = new ArrayList<String>();
  19. Cursor c = getContentResolver().query(
  20. ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
  21. null, null);
  22. //获取通讯录的信息
  23. startManagingCursor(c);
  24. int phoneIndex = 0, nameIndex = 0;
  25. if (c.getCount() > 0) {
  26. phoneIndex = c
  27. .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
  28. // 获取手机号码的列名
  29. nameIndex = c
  30. .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
  31. // 获取用户名的列名
  32. }
  33. while (c.moveToNext()) {
  34. String phone = c.getString(phoneIndex);
  35. // 获取手机号码
  36. list_phone.add(phone);
  37. String name = c.getString(nameIndex);
  38. // 获取用户名
  39. list_name.add(name);
  40. }
  41. ListViewAdapter adapter = new ListViewAdapter(this, list_name,
  42. list_phone);
  43. lv.setAdapter(adapter);
  44. }
  45. }

代码下载地址:
http://download.csdn.net/detail/zhengyikuangge/9511249

发表评论

表情:
评论列表 (有 0 条评论,274人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Android ListView

    ListView作为android的经常使用的控件,在使用时,经常会忘记一些使用要点,这里记录一下一些使用过程中的需求,以便后面使用查询: 设置 监听 使用