一、配置清單添加須要的權限android
代碼數據庫
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
<uses-permission android:name="android.permission.READ_CALL_LOG"/>數組
========================ide
二、佈局文件佈局
activity_main.xml佈局文件this
代碼xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >對象
<ListView
android:id="@+id/listView_main_calllist"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>字符串
<TextView
android:id="@+id/text_main_empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#00f"
android:textSize="24sp"
android:gravity="center"
android:text="暫無通話記錄信息!" />get
</LinearLayout>
-------------------------------------
item_listview.xml佈局文件
代碼
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text_item_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/text_item_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/text_item_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
========================================
三、MainActivity 類
代碼
public class MainActivity extends Activity {
private ListView listview;
private SimpleAdapter adapter;
private TextView text_empty;
private String uri_call = "content://call_log/calls";//訪問電話記錄的路徑
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.listview = (ListView) this
.findViewById(R.id.listView_main_calllist);
this.text_empty = (TextView) this.findViewById(R.id.text_main_empty);
listview.setEmptyView(text_empty);
/*使用ContentResolver 管理聯繫人: *(一)、 使用ContentResolver 操做數據的步驟:一、調用Context的getContentResolver()方法得到ContentResolver 對象二、調用使用ContentResolver 的insert()、delete()、update()、query()方法操做數據。Uri insert(Uri uri, ContentValues values)int delete(Uri uri, String where, String[] whereArgs)int update(Uri uri, ContentValues values, String where, String[] whereArgs)Cursor query(Uri uri, String[] projection, String where, String[] whereArgs, String sortOrder)參數解釋:String where:表示帶有佔位符的where子句組成的字符串;String[] whereArgs:表示替換where參數中佔位符後的數據組成的字符串數組;String sortOrder:表示select語句中的order by子句組成的字符串;String[] projection:表示select語句中須要查詢的全部的字段組成的字符串數組。ContentValues values:是由數據庫中表字段和往該字段中放置的數據所組成的鍵值對對象。 【備註:】以上四個方法的參數分別是二、三、四、5個。*/ ContentResolver resolver = getContentResolver(); Cursor cursor = resolver.query(Uri.parse(uri_call), new String[] { "_id", "number", "date", "type" }, null, null, null); List<Map<String, String>> list_calldata = select(cursor); adapter = new SimpleAdapter(this, list_calldata, R.layout.item_listview, new String[] {"number","date","type"}, new int[] {R.id.text_item_number,R.id.text_item_date,R.id.text_item_type}); listview.setAdapter(adapter); } public List<Map<String, String>> select(Cursor cursor){ List<Map<String, String>> list = new ArrayList<Map<String,String>>(); while(cursor.moveToNext()){//數據庫表的 行 Log.i("data", "dsfasfa" ); Map<String, String> map = new HashMap<String, String>(); for(int i = 0;i<cursor.getColumnCount();i++){//數據庫表的列 map.put(cursor.getColumnName(i), cursor.getString(i)); } list.add(map); } cursor.close(); return list; }}