標籤:ima hide params splay bubuko parent lte 儲存 ase
使用ListView顯示手機中連絡人的姓名和電話號碼
父類布局activity_main.xml,子類布局line.xml(一個檔案的單獨存放)
運行:
(避免泄露資訊對部分地方進行了塗鴉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>
AndroidManifest.xml
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); }}
MainActivity
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; }}
ListViewAdapter
<?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>
activity_main.xml
<?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>
item.xml
一、擷取手機儲存卡許可權
讀取通訊錄許可權
<uses-permission android:name="android.permission.READ_CONTACTS" />
二、介面布局
activity_main.xml布局中
TextView中顯示手機連絡人的的文字框,ListView列出手機中的連絡人
item.xml布局中
兩個TextView,一個顯示連絡人的姓名,一個顯示連絡人的電話號碼
三、實現程式功能
1、擷取手機電話號碼和連絡人資訊
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語句裡面的問號;
第五個參數:表示排序方式;
2、添加BaseAdapter適配器
simpleAdapter的擴充性最好,可以自訂各種各樣的布局出來,可以放上ImageView(圖片),還可以放上Button(按鈕),CheckBox(複選框)等等 ListView和Adapter資料配接器的簡單介紹:傳送門
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等)。
Android_(控制項)使用ListView顯示Android系統中連絡人資訊