使用ListView顯示手機中聯繫人的姓名和電話號碼html
父類佈局activity_main.xml,子類佈局line.xml(一個文件的單獨存放)java
運行截圖:android
(避免泄露信息對部分地方進行了塗鴉O(∩_∩)O!)數據庫
程序結構數組
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.asus.a7gary03"> <!-- 讀取通信錄權限 --> <uses-permission android:name="android.permission.READ_CONTACTS" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
package com.example.asus.a7gary03; import android.support.v7.app.AppCompatActivity; import android.database.Cursor; import android.os.Bundle; import android.provider.ContactsContract; import android.widget.ListView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { ListView lv; List<String> list_phone, list_name; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.lv); list_name = new ArrayList<String>(); list_phone = new ArrayList<String>(); Cursor c = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); //獲取通信錄的信息 startManagingCursor(c); int phoneIndex = 0, nameIndex = 0; if (c.getCount() > 0) { phoneIndex = c .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); // 獲取手機號碼的列名 nameIndex = c .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); // 獲取用戶名的列名 } while (c.moveToNext()) { String phone = c.getString(phoneIndex); // 獲取手機號碼 list_phone.add(phone); String name = c.getString(nameIndex); // 獲取用戶名 list_name.add(name); } ListViewAdapter adapter = new ListViewAdapter(this, list_name, list_phone); lv.setAdapter(adapter); } }
package com.example.asus.a7gary03; /** * Created by ASUS on 2018/5/24. */ import java.util.List; import android.annotation.SuppressLint; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; @SuppressLint("InflateParams") public class ListViewAdapter extends BaseAdapter { List<String> names, phones; LayoutInflater inflater; @SuppressWarnings("static-access") public ListViewAdapter(Context context, List<String> names, List<String> phones) { inflater = inflater.from(context); this.names = names; this.phones = phones; } @Override public int getCount() { return names.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view; if (convertView == null) { view = inflater.inflate(R.layout.item, null); TextView tv_name = (TextView) view.findViewById(R.id.tv_name); TextView tv_phone = (TextView) view.findViewById(R.id.tv_number); tv_name.setText(names.get(position)); tv_phone.setText(phones.get(position)); } else { view = convertView; } return view; } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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="com.example.asus.a7gary03.MainActivity"> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="60sp" android:orientation="horizontal" > <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="1111" android:textSize="20sp" /> <TextView android:id="@+id/tv_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:text="2222" android:textSize="15sp" /> </LinearLayout> </LinearLayout>
1、獲取手機存儲卡權限app
讀取通信錄權限
<uses-permission android:name="android.permission.READ_CONTACTS" />
2、界面佈局ide
activity_main.xml佈局中佈局
TextView中顯示手機聯繫人的的文本框,ListView列出手機中的聯繫人this
item.xml佈局中spa
兩個TextView,一個顯示聯繫人的姓名,一個顯示聯繫人的電話號碼
3、實現程序功能
一、獲取手機電話號碼和聯繫人信息
Cursor c = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); //獲取通信錄的信息 startManagingCursor(c); int phoneIndex = 0, nameIndex = 0; if (c.getCount() > 0) { phoneIndex = c .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); // 獲取手機號碼的列名 nameIndex = c .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); // 獲取用戶名的列名 } while (c.moveToNext()) { String phone = c.getString(phoneIndex); // 獲取手機號碼 list_phone.add(phone); String name = c.getString(nameIndex); // 獲取用戶名 list_name.add(name); }
Cursor:Android使用的數據庫是SQLite數據庫,對於數據庫記錄的操做,可使用Cursor來進行 (傳送門)
getColumnIndex(String columnName) -------->返回指定列的名稱 ,若是不存在返回-1
Cursor c = getContentResolver.query(uri , String[ ] , where , String[ ] , sort);
這條語句相信你們必定常常看到用到,查看sdk幫助文檔也很容易找到其中五個參數的意思
第一個參數:是一個URI,指向須要查詢的表;
第二個參數:須要查詢的列名,是一個數組,能夠返回多個列;
第三個參數:須要查詢的行,where表示須要知足的查詢條件,where語句裏面能夠有?號;
第四個參數:是一個數組,用來替代上面where語句裏面的問號;
第五個參數:表示排序方式;
public View getView(int position, View convertView, ViewGroup parent) { View view; if (convertView == null) { view = inflater.inflate(R.layout.item, null); TextView tv_name = (TextView) view.findViewById(R.id.tv_name); TextView tv_phone = (TextView) view.findViewById(R.id.tv_number); tv_name.setText(names.get(position)); tv_phone.setText(phones.get(position)); } else { view = convertView; } return view; }
LayoutInflater類的做用相似於findViewById()
不一樣點是LayoutInflater是用來找res/layout/下的xml佈局文件,而且實例化
而findViewById()是找xml佈局文件下的具體widget控件(如Button、TextView等)。